[SOLVED] Wait for timeout or some signal
-
wrote on 3 Jan 2012, 09:33 last edited by
Hi All!
I need send some message by the local net, and than need wait to some answer or exit at the function by timeout,
I try stop by forever loop, and there wait some signals, but no signal is comming (i think thats because i create this loop at the main thread), i want to try create wait loop and timeout timer not at the main threads, but i don't like this idea, some one can tall me easy way to resolve this problem? -
@
forever {
doMyLogic();
qApp->processEvents();
}
@ -
wrote on 3 Jan 2012, 09:46 last edited by
WOW so easy? i'll try that :) thnx
-
Yeah, it forces the QCoreApplication instance to process events in the event queue, including signals and slots provided by MOC. This way, the loop does not block operation of the app.
-
wrote on 3 Jan 2012, 09:48 last edited by
tnx, i note that!
-
wrote on 3 Jan 2012, 10:30 last edited by
Or, look into "QxtSignalWaiter":http://libqxt.bitbucket.org/doc/0.6/qxtsignalwaiter.html from the libQxt library.
-
wrote on 3 Jan 2012, 10:37 last edited by
tnx a lot!
-
wrote on 3 Jan 2012, 12:13 last edited by
Please don't use processEvents() like this, or your CPU usage will be terrible. Instead use QEventLoop.
-
wrote on 3 Jan 2012, 13:13 last edited by
Calling processEvents() in a loop like proposed will keep your CPU 100% bussy, because you just keep polling the event queue. Using an eventloop is a smarter solution that doesn't waste CPU cycles like that, so it will not spike your CPU. It is also the solution implemented by the libQxt in QxtSignalWaiter.
-
wrote on 3 Jan 2013, 15:32 last edited by
QxtSignalWaiter is implemented with a help of processEvents, not eventLoop. See "here":http://dev.libqxt.org/libqxt/src/21ea5919eabb2a404eafdffcdb8383472ca66bdd/src/core/qxtsignalwaiter.cpp?at=master#cl-143.
-
wrote on 3 Jan 2013, 15:35 last edited by
[quote author="RomaHagen" date="1357227120"]QxtSignalWaiter is implemented with a help of processEvents, not eventLoop. See "here":http://dev.libqxt.org/libqxt/src/21ea5919eabb2a404eafdffcdb8383472ca66bdd/src/core/qxtsignalwaiter.cpp?at=master#cl-143. [/quote]
Hmmm... You're right. Surprising. Thanks for the correction.
Does anyone know why Qxt choose this implementation instead?
-
wrote on 3 Jan 2013, 19:46 last edited by
See "this":http://dev.libqxt.org/libqxt/src/21ea5919eabb2a404eafdffcdb8383472ca66bdd/src/core/qxtsignalwaiter.cpp?at=master#cl-143 and read the doc about WaitForMoreEvents "here":http://qt-project.org/doc/qt-4.8/qeventloop.html#ProcessEventsFlag-enum.
-
wrote on 4 Jan 2013, 08:09 last edited by
Yes, I know where to find the sources and I know the docs, but that was not my question. I sometimes use this idea:
@
QEventLoop loop;
connect(myObject, SIGNAL(theSignalToWaitFor()), &loop, SLOT(quit()));
connect(timeoutTimer, SIGNAL(timeout()), &loop, SLOT(quit()));
loop.exec(); //blocks untill either theSignalToWaitFor or timeout was fired
@ -
wrote on 31 Oct 2014, 09:54 last edited by
[quote author="Andre" date="1357286986"]Yes, I know where to find the sources and I know the docs, but that was not my question. I sometimes use this idea:
@
QEventLoop loop;
connect(myObject, SIGNAL(theSignalToWaitFor()), &loop, SLOT(quit()));
connect(timeoutTimer, SIGNAL(timeout()), &loop, SLOT(quit()));
loop.exec(); //blocks untill either theSignalToWaitFor or timeout was fired
@[/quote]
thank you. it's very useful.