<?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: C# .NET &#8211; UI Threading Example</title>
	<atom:link href="http://dougzuck.com/c-ui-threading-example/feed" rel="self" type="application/rss+xml" />
	<link>http://dougzuck.com/c-ui-threading-example</link>
	<description>A place to put some stuff...</description>
	<lastBuildDate>Sat, 04 Feb 2012 00:13:01 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: doug</title>
		<link>http://dougzuck.com/c-ui-threading-example#comment-52186</link>
		<dc:creator>doug</dc:creator>
		<pubDate>Thu, 17 Nov 2011 20:59:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougzuck.com/?p=396#comment-52186</guid>
		<description>Raj - thanks for the comment.  I am definitely not an expert, but I don&#039;t think we need a lock here.  In fact, you&#039;ll find numerous articles on the web that specifically state to never lock(this). 

Additionally, I think it&#039;s the case that when we use this.Invoke, we actually automatically put ourselves in a thread-safe situation.  The Invoke method executes the method on the control&#039;s thread, which I think means that even if you have 20 different threads simultaneously use this.Invoke, they will all get executed sequentially and safely.  However, again I admit that I&#039;m not an expert on the subject.  Nevertheless, I&#039;ve never run into a problem with the method used in the above code, so I feel pretty good about it being a safe/good way to handle the textbox update.

I&#039;m always open to learn new things, so if anyone has additional light to shed, please do.

-Doug</description>
		<content:encoded><![CDATA[<p>Raj &#8211; thanks for the comment.  I am definitely not an expert, but I don&#8217;t think we need a lock here.  In fact, you&#8217;ll find numerous articles on the web that specifically state to never lock(this). </p>
<p>Additionally, I think it&#8217;s the case that when we use this.Invoke, we actually automatically put ourselves in a thread-safe situation.  The Invoke method executes the method on the control&#8217;s thread, which I think means that even if you have 20 different threads simultaneously use this.Invoke, they will all get executed sequentially and safely.  However, again I admit that I&#8217;m not an expert on the subject.  Nevertheless, I&#8217;ve never run into a problem with the method used in the above code, so I feel pretty good about it being a safe/good way to handle the textbox update.</p>
<p>I&#8217;m always open to learn new things, so if anyone has additional light to shed, please do.</p>
<p>-Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: doug</title>
		<link>http://dougzuck.com/c-ui-threading-example#comment-51025</link>
		<dc:creator>doug</dc:creator>
		<pubDate>Mon, 31 Oct 2011 23:42:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougzuck.com/?p=396#comment-51025</guid>
		<description>Interesting findings.  Thanks for sharing.  

One point about preventing the exception when closing the form before the worker thread completes:  Microsoft appears to acknowledge that this is expected behavior, if I&#039;m reading the following page correctly.  

They say: &quot;An exception might be thrown if the thread that should process the message is no longer active.&quot;  

http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

So, perhaps the best thing to do is put the this.Invoke call inside a try {} block.  I haven&#039;t tested that, but I&#039;d imagine it might work.

-Doug</description>
		<content:encoded><![CDATA[<p>Interesting findings.  Thanks for sharing.  </p>
<p>One point about preventing the exception when closing the form before the worker thread completes:  Microsoft appears to acknowledge that this is expected behavior, if I&#8217;m reading the following page correctly.  </p>
<p>They say: &#8220;An exception might be thrown if the thread that should process the message is no longer active.&#8221;  </p>
<p><a href="http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx</a></p>
<p>So, perhaps the best thing to do is put the this.Invoke call inside a try {} block.  I haven&#8217;t tested that, but I&#8217;d imagine it might work.</p>
<p>-Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sunik</title>
		<link>http://dougzuck.com/c-ui-threading-example#comment-50995</link>
		<dc:creator>sunik</dc:creator>
		<pubDate>Mon, 31 Oct 2011 07:02:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougzuck.com/?p=396#comment-50995</guid>
		<description>Quoting Jim, since he pretty much said the exact same thing I wanted to say, plus couple of interesting findings;

I use C# 2010 Express, and I noticed couple of things different from this example which was done in VC#2008:
- using a direct call populateTextBox(count.ToString()); in workerFunction method using new thread doesn&#039;t yield &quot;funky&quot; results; instead, it gives &quot;InvalidOperationException&quot; saying &#039;textBox1&#039; cannot be &quot;accessed from a thread other than the thread it was created on.&quot;
- the output of &quot;wrong way&quot; to execute worker function is different than one described here( At the end of 15 seconds the UI becomes responsive again and you see a “15″ in the text box.). Instead, at the end of 15 seconds, you see the whole series &quot;1 2 3 4 (...) 15&quot; appear at once.

As per Frank&#039;s concern, I found placing lock around worker function seem to make the problem go away. I am not at all sure if this is a legitimate solution, since removing the lock doesn&#039;t necessarily bring the problem back; I&#039;d love to see some1 answer his/my question someday.

Thanks again Doug</description>
		<content:encoded><![CDATA[<p>Quoting Jim, since he pretty much said the exact same thing I wanted to say, plus couple of interesting findings;</p>
<p>I use C# 2010 Express, and I noticed couple of things different from this example which was done in VC#2008:<br />
- using a direct call populateTextBox(count.ToString()); in workerFunction method using new thread doesn&#8217;t yield &#8220;funky&#8221; results; instead, it gives &#8220;InvalidOperationException&#8221; saying &#8216;textBox1&#8242; cannot be &#8220;accessed from a thread other than the thread it was created on.&#8221;<br />
- the output of &#8220;wrong way&#8221; to execute worker function is different than one described here( At the end of 15 seconds the UI becomes responsive again and you see a “15″ in the text box.). Instead, at the end of 15 seconds, you see the whole series &#8220;1 2 3 4 (&#8230;) 15&#8243; appear at once.</p>
<p>As per Frank&#8217;s concern, I found placing lock around worker function seem to make the problem go away. I am not at all sure if this is a legitimate solution, since removing the lock doesn&#8217;t necessarily bring the problem back; I&#8217;d love to see some1 answer his/my question someday.</p>
<p>Thanks again Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: francisco</title>
		<link>http://dougzuck.com/c-ui-threading-example#comment-15415</link>
		<dc:creator>francisco</dc:creator>
		<pubDate>Wed, 01 Sep 2010 20:10:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougzuck.com/?p=396#comment-15415</guid>
		<description>i was stuck with my degree project because of the &quot;only UI thread can modify UI elements&quot; problem. thanks!! :)</description>
		<content:encoded><![CDATA[<p>i was stuck with my degree project because of the &#8220;only UI thread can modify UI elements&#8221; problem. thanks!! :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: doug</title>
		<link>http://dougzuck.com/c-ui-threading-example#comment-11199</link>
		<dc:creator>doug</dc:creator>
		<pubDate>Tue, 27 Jul 2010 01:16:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougzuck.com/?p=396#comment-11199</guid>
		<description>Glad to hear it, Jim!  Thanks.</description>
		<content:encoded><![CDATA[<p>Glad to hear it, Jim!  Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim</title>
		<link>http://dougzuck.com/c-ui-threading-example#comment-11186</link>
		<dc:creator>Jim</dc:creator>
		<pubDate>Mon, 26 Jul 2010 23:19:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougzuck.com/?p=396#comment-11186</guid>
		<description>Hi Doug,

Just a quick Thanks!! for posting your article. After digging through the C# 4.0 Nutshell book and numerous examples on the &#039;net, yours is the first example that is straightforward, understandable, and actually worked when I used it as an example in my thread-to-UI update code.  Jim</description>
		<content:encoded><![CDATA[<p>Hi Doug,</p>
<p>Just a quick Thanks!! for posting your article. After digging through the C# 4.0 Nutshell book and numerous examples on the &#8216;net, yours is the first example that is straightforward, understandable, and actually worked when I used it as an example in my thread-to-UI update code.  Jim</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frank</title>
		<link>http://dougzuck.com/c-ui-threading-example#comment-1993</link>
		<dc:creator>Frank</dc:creator>
		<pubDate>Mon, 18 Jan 2010 13:53:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougzuck.com/?p=396#comment-1993</guid>
		<description>Hi, I like the simplicity of your example. However, I wonder how the following situation should be resolved:

If you start the &quot;worker in a new thread&quot; and close the form BEFORE it has completed, the application crashes. 

Any idea on how to deal with this situation?</description>
		<content:encoded><![CDATA[<p>Hi, I like the simplicity of your example. However, I wonder how the following situation should be resolved:</p>
<p>If you start the &#8220;worker in a new thread&#8221; and close the form BEFORE it has completed, the application crashes. </p>
<p>Any idea on how to deal with this situation?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Al</title>
		<link>http://dougzuck.com/c-ui-threading-example#comment-1237</link>
		<dc:creator>Al</dc:creator>
		<pubDate>Mon, 23 Nov 2009 19:04:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougzuck.com/?p=396#comment-1237</guid>
		<description>i think the lock{} is only needed for shared variables. i.e. if you have more than one thread writing to that textbox. if there&#039;s another thread writing to that same text box you want to make sure it waits until the first thread finishes. to keep concurrency.</description>
		<content:encoded><![CDATA[<p>i think the lock{} is only needed for shared variables. i.e. if you have more than one thread writing to that textbox. if there&#8217;s another thread writing to that same text box you want to make sure it waits until the first thread finishes. to keep concurrency.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raj</title>
		<link>http://dougzuck.com/c-ui-threading-example#comment-1116</link>
		<dc:creator>Raj</dc:creator>
		<pubDate>Sun, 15 Nov 2009 01:58:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougzuck.com/?p=396#comment-1116</guid>
		<description>Dont we need to put the method populateTextBox inside a lock ?

      void populateTextBox(string text)
       {
          lock(this)
            {
                  textBox1.Text = textBox1.Text + &quot; &quot; + text;
             }
       }</description>
		<content:encoded><![CDATA[<p>Dont we need to put the method populateTextBox inside a lock ?</p>
<p>      void populateTextBox(string text)<br />
       {<br />
          lock(this)<br />
            {<br />
                  textBox1.Text = textBox1.Text + &#8221; &#8221; + text;<br />
             }<br />
       }</p>
]]></content:encoded>
	</item>
</channel>
</rss>

