<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Making asynchronous calls work like synchronous calls]]></title><description><![CDATA[<p dir="auto">Dear Qt users,<br />
I know this topic has been discussed all over the web. The usual suggestion is to use this: @QEventLoop loop;<br />
loop.connect(object, SIGNAL(operationCompleted()), SLOT(quit()));<br />
object-&gt;startOperation();<br />
loop.exec(QEventLoop::AllEvents|QEventLoop::WaitForMoreEvents);@ However I was wondering: what happens if the signal is emitted before we call <em>loop.exec()</em>?<br />
Could we stay stuck in the exec()? Particularly if the startOperation() calls processEvents?</p>
<p dir="auto">I have used this trick a few times in the past, but I'm now struggling with an asynchronous activex.<br />
I can confirm that the operationCompleted() signal is emitted. However the loop never quits.<br />
I've noticed that startOperation() takes a little time (500msec) and I don't know what happens there.</p>
]]></description><link>https://forum.qt.io/topic/23550/making-asynchronous-calls-work-like-synchronous-calls</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 02:58:56 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/23550.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 23 Jan 2013 18:05:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Making asynchronous calls work like synchronous calls on Thu, 24 Jan 2013 14:55:28 GMT]]></title><description><![CDATA[<p dir="auto">Andre ... you rock!</p>
]]></description><link>https://forum.qt.io/post/164461</link><guid isPermaLink="true">https://forum.qt.io/post/164461</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Thu, 24 Jan 2013 14:55:28 GMT</pubDate></item><item><title><![CDATA[Reply to Making asynchronous calls work like synchronous calls on Thu, 24 Jan 2013 14:07:13 GMT]]></title><description><![CDATA[<p dir="auto">You might also be interested in the so-called "Delta Object Rules":<a href="http://delta.affinix.com/dor/" target="_blank" rel="noopener noreferrer nofollow ugc">http://delta.affinix.com/dor/</a> These rules, especially the second one, specifically describes the issue you run into where a finished() signal is send immediately after a request().</p>
<p dir="auto">I find it a good read.</p>
<p dir="auto">If you want to wait for one or more signals, you could also take a look at libQxt. There is a "QxtSignalWaiter":<a href="http://libqxt.bitbucket.org/doc/0.6/qxtsignalwaiter.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://libqxt.bitbucket.org/doc/0.6/qxtsignalwaiter.html</a> class that does all you need.</p>
]]></description><link>https://forum.qt.io/post/164445</link><guid isPermaLink="true">https://forum.qt.io/post/164445</guid><dc:creator><![CDATA[andre]]></dc:creator><pubDate>Thu, 24 Jan 2013 14:07:13 GMT</pubDate></item><item><title><![CDATA[Reply to Making asynchronous calls work like synchronous calls on Thu, 24 Jan 2013 08:42:49 GMT]]></title><description><![CDATA[<p dir="auto">just connect the timeout to another slot which then notifies you about the timeout.</p>
]]></description><link>https://forum.qt.io/post/164382</link><guid isPermaLink="true">https://forum.qt.io/post/164382</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Thu, 24 Jan 2013 08:42:49 GMT</pubDate></item><item><title><![CDATA[Reply to Making asynchronous calls work like synchronous calls on Thu, 24 Jan 2013 08:37:32 GMT]]></title><description><![CDATA[<p dir="auto">Would be nice to be able to determine if the QEventLoop has been stopped by the signal or by the timeout.</p>
]]></description><link>https://forum.qt.io/post/164378</link><guid isPermaLink="true">https://forum.qt.io/post/164378</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Thu, 24 Jan 2013 08:37:32 GMT</pubDate></item><item><title><![CDATA[Reply to Making asynchronous calls work like synchronous calls on Thu, 24 Jan 2013 07:59:36 GMT]]></title><description><![CDATA[<p dir="auto">If you have a look at "this article":<a href="http://www.developer.nokia.com/Community/Wiki/How_to_wait_synchronously_for_a_Signal_in_Qt" target="_blank" rel="noopener noreferrer nofollow ugc">http://www.developer.nokia.com/Community/Wiki/How_to_wait_synchronously_for_a_Signal_in_Qt</a> you will see that they do not recommend the approach for real applications. There are a number of problem that can arise if you are not 100% sure of what you are doing (race condition, recursive event loops,...).</p>
<p dir="auto">Also I would recommend to always use a Timer for having the eventloop time out after a certain time.<br />
@<br />
QEventLoop loop;<br />
QTimer timer;<br />
timer.setInterval(YOUR_TIMEOUT_IN_MILLISECS);<br />
timer.setSingleShot(true);<br />
connect(&amp;timer, SIGNAL(timeout()), &amp;loop, SLOT(quit()));<br />
connect(object, SIGNAL(operationCompleted()), &amp;loop, SLOT(quit()));<br />
QTimer::singleShot(0, object, SLOT(startOperation()));<br />
loop.exec(QEventLoop::AllEvents|QEventLoop::WaitForMoreEvents);<br />
@</p>
]]></description><link>https://forum.qt.io/post/164372</link><guid isPermaLink="true">https://forum.qt.io/post/164372</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Thu, 24 Jan 2013 07:59:36 GMT</pubDate></item><item><title><![CDATA[Reply to Making asynchronous calls work like synchronous calls on Wed, 23 Jan 2013 20:38:49 GMT]]></title><description><![CDATA[<p dir="auto">Ok, then is it right to say that the original piece of code from my first post is another example of "You are doing it wrong"?<br />
If yes, why can we find this example here (last line of the table)<br />
<a href="http://www.developer.nokia.com/Community/Wiki/TSQ001335_-_Asynchronous_operations_in_S60_and_Qt" target="_blank" rel="noopener noreferrer nofollow ugc">http://www.developer.nokia.com/Community/Wiki/TSQ001335_-_Asynchronous_operations_in_S60_and_Qt</a></p>
]]></description><link>https://forum.qt.io/post/164327</link><guid isPermaLink="true">https://forum.qt.io/post/164327</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Wed, 23 Jan 2013 20:38:49 GMT</pubDate></item><item><title><![CDATA[Reply to Making asynchronous calls work like synchronous calls on Wed, 23 Jan 2013 20:32:31 GMT]]></title><description><![CDATA[<p dir="auto">What about Qt::QueuedConnection and Qt::BlockingQueuedConnection?</p>
]]></description><link>https://forum.qt.io/post/164326</link><guid isPermaLink="true">https://forum.qt.io/post/164326</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 23 Jan 2013 20:32:31 GMT</pubDate></item><item><title><![CDATA[Reply to Making asynchronous calls work like synchronous calls on Wed, 23 Jan 2013 20:23:56 GMT]]></title><description><![CDATA[<p dir="auto">If your <em>startOperation</em> method contains an "emit operationCompleted()", then it will immediately trigger the quit slot of your event loop. When you write "emit signal()", in a single threaded application, all slots connected are called immediately, not posted.</p>
<p dir="auto">To change this behaviour, you can use the fourth argument of the connect method: "connect (&amp;sender, sig, &amp;receiver, slot, Qt::QueuedConnection) ":<a href="http://doc.qt.digia.com/qt/qobject.html#connect" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.digia.com/qt/qobject.html#connect</a></p>
<p dir="auto">So in your case, it could look like...<br />
@<br />
myfunction::startProcessing<br />
-&gt; object::startOperation -&gt; emit operationCompleted() -&gt; QEventLoop::quit() // Does nothing<br />
-&gt; QEventLoop::exec() // Will never quit<br />
@</p>
<p dir="auto">The same issue arises if you call processEvents() in you operationCompleted, even with an asynchronous connection.</p>
<p dir="auto">The single shot timer should be used just before the exec(). That way, the event is posted, and the <em>startOperation</em> slot will be executed "from inside" the event loop.<br />
@<br />
QEventLoop loop;<br />
loop.connect(object, SIGNAL(operationCompleted()), SLOT(quit()));<br />
QTimer::singleShot(0, object, SLOT(startOperation());<br />
loop.exec(QEventLoop::AllEvents|QEventLoop::WaitForMoreEvents);<br />
@</p>
<p dir="auto">Bonne soirée :)</p>
]]></description><link>https://forum.qt.io/post/164323</link><guid isPermaLink="true">https://forum.qt.io/post/164323</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 23 Jan 2013 20:23:56 GMT</pubDate></item><item><title><![CDATA[Reply to Making asynchronous calls work like synchronous calls on Wed, 23 Jan 2013 19:35:26 GMT]]></title><description><![CDATA[<p dir="auto">Bonsoir Adrien.<br />
Could you please detail the real reason for getting stuck in exec()?<br />
As long as object-&gt;startOperation() does not handle an event loop, the emitted signal should be queued and processed by the exec() even if it emitted to early, right?</p>
<p dir="auto">Regarding your solution, again, could you detail it? Do you suggest this: @QEventLoop loop;<br />
loop.connect(object, SIGNAL(operationCompleted()), SLOT(quit()));<br />
loop.exec(QEventLoop::AllEvents|QEventLoop::WaitForMoreEvents);<br />
QTimer::singleShot(0, object, SLOT(startOperation());@ Cause it sounds like the singleShot will never be reached!</p>
]]></description><link>https://forum.qt.io/post/164319</link><guid isPermaLink="true">https://forum.qt.io/post/164319</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Wed, 23 Jan 2013 19:35:26 GMT</pubDate></item><item><title><![CDATA[Reply to Making asynchronous calls work like synchronous calls on Wed, 23 Jan 2013 19:29:08 GMT]]></title><description><![CDATA[<p dir="auto">Hi Julien M,</p>
<p dir="auto">You could definitively stay stuck in the exec() if the "operationCompleted" signal is triggered. That is because signals are processed synchroneously in a single thread application, or if you call processEvent like you said.</p>
<p dir="auto">To avoid this issue, you can you a QTimer::singleShot(0, object, SLOT ( startOperation() ) to trigger the start immediately after exec is called (after preceding events of course). Using "0" as duration posts an event that is processed the next event loop iteration.</p>
]]></description><link>https://forum.qt.io/post/164318</link><guid isPermaLink="true">https://forum.qt.io/post/164318</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 23 Jan 2013 19:29:08 GMT</pubDate></item></channel></rss>