<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: Waiting for asynchronous events	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=waiting-for-asynchronous-events</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Fri, 30 Mar 2018 09:19:54 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-421732</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Fri, 30 Mar 2018 09:19:54 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3017#comment-421732</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-414166&quot;&gt;Ben A.&lt;/a&gt;.

Unfortunately, using the Python object requires a separate installation of Python. While it is true that many users already have Python installed, many others do not and for such users the Python solution will error. In contrast, the solution that I presented above is based on the built-in &lt;b&gt;&lt;i&gt;waitfor&lt;/i&gt;&lt;/b&gt; function, and therefore works for all users.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-414166">Ben A.</a>.</p>
<p>Unfortunately, using the Python object requires a separate installation of Python. While it is true that many users already have Python installed, many others do not and for such users the Python solution will error. In contrast, the solution that I presented above is based on the built-in <b><i>waitfor</i></b> function, and therefore works for all users.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-421731</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Fri, 30 Mar 2018 09:15:54 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3017#comment-421731</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-410305&quot;&gt;Brian&lt;/a&gt;.

I discussed Ctrl-C interrupts here: http://undocumentedmatlab.com/blog/mex-ctrl-c-interrupt]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-410305">Brian</a>.</p>
<p>I discussed Ctrl-C interrupts here: <a href="http://undocumentedmatlab.com/blog/mex-ctrl-c-interrupt" rel="ugc">http://undocumentedmatlab.com/blog/mex-ctrl-c-interrupt</a></p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Ben A.		</title>
		<link>https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-414166</link>

		<dc:creator><![CDATA[Ben A.]]></dc:creator>
		<pubDate>Fri, 29 Sep 2017 16:55:16 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3017#comment-414166</guid>

					<description><![CDATA[Hey Yair,
These solutions have proven to be invaluable for me, but sometimes don&#039;t work well for complicated asynchronous situations.  I have found an additional approach that has worked very well for me so far.
Python (which we can call natively in MATLAB) has a threading module with an Event object that works easily and reliably.  Creating the Event object is just a matter of calling:
&lt;pre lang=&quot;matlab&quot;&gt;myevent = py.threading.Event();&lt;/pre&gt;
And then making the object available to all interested parties (through appdata, parameter passing, etc.).  
The object&#039;s set(), is_set(), clear(), and wait() functions work quickly and reliably in MATLAB, and the wait() function takes a timeout parameter and returns a Boolean value that is true unless the timeout was reached.]]></description>
			<content:encoded><![CDATA[<p>Hey Yair,<br />
These solutions have proven to be invaluable for me, but sometimes don&#8217;t work well for complicated asynchronous situations.  I have found an additional approach that has worked very well for me so far.<br />
Python (which we can call natively in MATLAB) has a threading module with an Event object that works easily and reliably.  Creating the Event object is just a matter of calling:</p>
<pre lang="matlab">myevent = py.threading.Event();</pre>
<p>And then making the object available to all interested parties (through appdata, parameter passing, etc.).<br />
The object&#8217;s set(), is_set(), clear(), and wait() functions work quickly and reliably in MATLAB, and the wait() function takes a timeout parameter and returns a Boolean value that is true unless the timeout was reached.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Brian		</title>
		<link>https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-410305</link>

		<dc:creator><![CDATA[Brian]]></dc:creator>
		<pubDate>Sun, 16 Jul 2017 04:10:39 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3017#comment-410305</guid>

					<description><![CDATA[Hey I know this is a rather old post, but I wanted to post my solution just in case anyone from the future has the same question. It turns out you can call back into Matlab via JMI to poll the keyboard event queue and trigger an exception if a &quot;Ctrl-C&quot; is waiting. I wrote this up in more detail on my blog at &lt;a href=&quot;https://v1ntage.io/2017/07/01/episode-001-handling-matlab-interrupts-in-java/&quot; rel=&quot;nofollow&quot;&gt;https://v1ntage.io/2017/07/01/episode-001-handling-matlab-interrupts-in-java/&lt;/a&gt;. Let me know what you think.]]></description>
			<content:encoded><![CDATA[<p>Hey I know this is a rather old post, but I wanted to post my solution just in case anyone from the future has the same question. It turns out you can call back into Matlab via JMI to poll the keyboard event queue and trigger an exception if a &#8220;Ctrl-C&#8221; is waiting. I wrote this up in more detail on my blog at <a href="https://v1ntage.io/2017/07/01/episode-001-handling-matlab-interrupts-in-java/" rel="nofollow">https://v1ntage.io/2017/07/01/episode-001-handling-matlab-interrupts-in-java/</a>. Let me know what you think.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Episode 001 &#8211; Handling Matlab interrupts in Java &#8211; v1ntage.io		</title>
		<link>https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-409516</link>

		<dc:creator><![CDATA[Episode 001 &#8211; Handling Matlab interrupts in Java &#8211; v1ntage.io]]></dc:creator>
		<pubDate>Sat, 01 Jul 2017 03:52:08 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3017#comment-409516</guid>

					<description><![CDATA[[&#8230;] that is currently blocked forever inside the Java method. This problem has come up again, and again, and again over the [&#8230;]]]></description>
			<content:encoded><![CDATA[<p>[&#8230;] that is currently blocked forever inside the Java method. This problem has come up again, and again, and again over the [&#8230;]</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-368918</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Mon, 01 Feb 2016 10:45:15 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3017#comment-368918</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-368890&quot;&gt;Johan&lt;/a&gt;.

The code that is launched by the button callback runs on a separate thread so &lt;i&gt;&lt;b&gt;waitfor&lt;/b&gt;&lt;/i&gt; simply blocks execution of that thread, and other callbacks can still process. This is actually documented in &lt;i&gt;&lt;b&gt;waitfor&lt;/b&gt;&lt;/i&gt;&#039;s documentation. I suggest to modify the callback code by disabling the button to prevent additional button clicks while one callback is blocked:

&lt;pre lang=&quot;matlab&quot;&gt;
function cbf(hButton,~)
  set(hButton,&#039;Enable&#039;,&#039;off&#039;);
  waitfor(msgbox(...))
  set(hButton,&#039;Enable&#039;,&#039;on&#039;);
end
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-368890">Johan</a>.</p>
<p>The code that is launched by the button callback runs on a separate thread so <i><b>waitfor</b></i> simply blocks execution of that thread, and other callbacks can still process. This is actually documented in <i><b>waitfor</b></i>&#8216;s documentation. I suggest to modify the callback code by disabling the button to prevent additional button clicks while one callback is blocked:</p>
<pre lang="matlab">
function cbf(hButton,~)
  set(hButton,'Enable','off');
  waitfor(msgbox(...))
  set(hButton,'Enable','on');
end
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Johan		</title>
		<link>https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-368890</link>

		<dc:creator><![CDATA[Johan]]></dc:creator>
		<pubDate>Mon, 01 Feb 2016 01:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3017#comment-368890</guid>

					<description><![CDATA[Hi Yair,

I stumbled onto this page when trying to solve my conundrum. I&#039;m trying to get &#039;waitfor/uiwait&#039; to hold for a message box in a callback. I made a minimum working example like this:

&lt;pre lang=&quot;matlab&quot;&gt;
function foo()
uicontrol(&#039;style&#039;,&#039;push&#039;,&#039;string&#039;,&#039;push&#039;,&#039;callback&#039;,@cbf,&#039;parent&#039;,figure);

function cbf(~,~)
h = msgbox(&#039;press ok to continue&#039;);
waitfor(h)
&lt;/pre&gt;

But no matter what I do, the waitfor is not respected, that is the pushbutton is not blocked so one can open as many msgboxes as one likes. Very odd behaviour I think, have you by any chance encountered issues like this before?]]></description>
			<content:encoded><![CDATA[<p>Hi Yair,</p>
<p>I stumbled onto this page when trying to solve my conundrum. I&#8217;m trying to get &#8216;waitfor/uiwait&#8217; to hold for a message box in a callback. I made a minimum working example like this:</p>
<pre lang="matlab">
function foo()
uicontrol('style','push','string','push','callback',@cbf,'parent',figure);

function cbf(~,~)
h = msgbox('press ok to continue');
waitfor(h)
</pre>
<p>But no matter what I do, the waitfor is not respected, that is the pushbutton is not blocked so one can open as many msgboxes as one likes. Very odd behaviour I think, have you by any chance encountered issues like this before?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-367967</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 20 Jan 2016 22:23:08 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3017#comment-367967</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-367966&quot;&gt;Marc&lt;/a&gt;.

This is not surprising. Matlab&#039;s computational engine is [still] single threaded. I hope and expect this will change in the future. Even when it does, I&#039;m not sure that the listeners would be dispatched asynchronously. I can think of quite a few use-cases where the synchronous listener invocation is actually intended.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-367966">Marc</a>.</p>
<p>This is not surprising. Matlab&#8217;s computational engine is [still] single threaded. I hope and expect this will change in the future. Even when it does, I&#8217;m not sure that the listeners would be dispatched asynchronously. I can think of quite a few use-cases where the synchronous listener invocation is actually intended.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Marc		</title>
		<link>https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-367966</link>

		<dc:creator><![CDATA[Marc]]></dc:creator>
		<pubDate>Wed, 20 Jan 2016 22:19:14 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3017#comment-367966</guid>

					<description><![CDATA[Related question. I&#039;m coding up a simulation, and I&#039;ve got a listener in one object that is dependent on the completion of the listener in another object.  (This is not gui work).  They are both listening to the same event from the same object.  In the dependent class, I tried busy-waiting (with a pause, of course), and it seems that no matter what I do, those two callbacks execute sequentially.  

based on this, I presume that matlab is executing all of these callbacks in a single thread, and the dispatcher for the event source is just executing the listeners in order, so that it can&#039;t process the next until the prior one returns?]]></description>
			<content:encoded><![CDATA[<p>Related question. I&#8217;m coding up a simulation, and I&#8217;ve got a listener in one object that is dependent on the completion of the listener in another object.  (This is not gui work).  They are both listening to the same event from the same object.  In the dependent class, I tried busy-waiting (with a pause, of course), and it seems that no matter what I do, those two callbacks execute sequentially.  </p>
<p>based on this, I presume that matlab is executing all of these callbacks in a single thread, and the dispatcher for the event source is just executing the listeners in order, so that it can&#8217;t process the next until the prior one returns?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-367962</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 20 Jan 2016 20:57:57 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3017#comment-367962</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-367959&quot;&gt;Marc Passy&lt;/a&gt;.

@Marc - however, you can still use &lt;i&gt;&lt;b&gt;waitfor&lt;/b&gt;&lt;/i&gt; with Java objects]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events#comment-367959">Marc Passy</a>.</p>
<p>@Marc &#8211; however, you can still use <i><b>waitfor</b></i> with Java objects</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
