<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9 Forums - Coffeehouse - Question on C# Threads</title>
	<atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Forums/rss"></atom:link>
	<image>
		<url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
		<title>Channel 9 Forums - Coffeehouse - Question on C# Threads</title>
		<link>http://channel9.msdn.com/Forums</link>
	</image>
	<description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
	<link>http://channel9.msdn.com/Forums</link>
	<language>en</language>
	<pubDate>Thu, 23 May 2013 18:46:37 GMT</pubDate>
	<lastBuildDate>Thu, 23 May 2013 18:46:37 GMT</lastBuildDate>
	<generator>Rev9</generator>
	<c9:totalResults>20</c9:totalResults>
	<c9:pageCount>-20</c9:pageCount>
	<c9:pageSize>-1</c9:pageSize>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>So I'm completely new to C# Threading and I'm having some trouble understanding some things. Currently I have a program that does something like this:</p><p><pre class="brush: csharp">Thread someThread = null;

void foo()
{
   someThread = new Thread(goo);
   someThread.Start();

}

void goo()
{
   ...
}

void bar()
{
   someThread.Join(); // Null Reference Exception
}</pre></p><p>I've made that sure that someThread.Start() is being called. I'm guessing it's being set to null after finishing, and it's finishing before I call Join. I'm not too sure how to deal with this, because there may be a chance that the thread is not yet finished, so I don't feel too good about removing the Join since it should be synchronous. Any ideas niners?</p><p>Thanks in advance.&nbsp; <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-1.gif?v=c9' alt='Smiley' /></p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/b7f14c5fc3f84b68b42fa0be010ea6de#b7f14c5fc3f84b68b42fa0be010ea6de</link>
		<pubDate>Fri, 31 Aug 2012 16:25:24 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/b7f14c5fc3f84b68b42fa0be010ea6de#b7f14c5fc3f84b68b42fa0be010ea6de</guid>
		<dc:creator>pavone</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/pavone/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>OK what do you want to do here? Fire off something into a background task, and wait for it to finish before continuing?</p><p>If that's the case you probably don't need to go as low level as the Thread class, there's a few other options, including BackgroundWorker, Task, Plinq, Async, Lions and Tigers oh may!</p><p>Personally I like Task, but then all my use cases are fire and forget. You can also chain tasks, tor example</p><p><pre class="brush: csharp">var startTask = new Task(() =&gt; SomeMethodSomewhere());

var nextTask = startTask.ContinueWith((t) =&gt; SomethingElseThatIsRunAfter());

startTask.Start();

</pre></p><p>Task.WaitAll() then gives you a way to wait for completion.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/962ccf13d36d44bd94f8a0be01111129#962ccf13d36d44bd94f8a0be01111129</link>
		<pubDate>Fri, 31 Aug 2012 16:34:12 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/962ccf13d36d44bd94f8a0be01111129#962ccf13d36d44bd94f8a0be01111129</guid>
		<dc:creator>blowdart</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/blowdart/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>if ( someThread != null &amp;&amp; someThread.IsStillRunningOrSomething() )</p><p>someThread.Join();</p><p>EDIT: Obviously, Blowdart's answer is better.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/281dabcf6de9460d9c1ba0be01133567#281dabcf6de9460d9c1ba0be01133567</link>
		<pubDate>Fri, 31 Aug 2012 16:42:00 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/281dabcf6de9460d9c1ba0be01133567#281dabcf6de9460d9c1ba0be01133567</guid>
		<dc:creator>ZippyV</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/ZippyV/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>If you need it to be synchronous, don't use the thread. Threads should be for tasks going on in the background, not for doing critical stuff that you need to wait for.</p><p>As a general rule, if Thread.Join is the answer, you've misunderstodd how to do threading.</p><p>I often use the pattern</p><p>class SomeClass<br>{<br>&nbsp; Thread bgThread;<br>&nbsp; event System.EventHandler SomethingCompleted;<br>&nbsp; public SomeClass()<br>&nbsp; {<br>&nbsp; &nbsp; bgThread = new Thread(() =&gt; { bgThreadMain(); });<br>&nbsp; }<br>&nbsp; public void StartBackgroundWork()<br>&nbsp; {<br>&nbsp; &nbsp; bgThread.Start();<br>&nbsp; }<br>&nbsp; private void bgThreadMain()<br>&nbsp; {<br>&nbsp; &nbsp; doSomethingThatMightTakeAWhile();<br>&nbsp; &nbsp; if(SomethingCompleted != null) SomethingCompleted(this, EventArgs.Empty);<br>&nbsp; }<br>&nbsp; private void doSomethingThatMightTakeAWhile()<br>&nbsp; {<br>&nbsp; &nbsp; Process.Start(&quot;ping&quot;, &quot;127.0.0.1&quot;);<br>&nbsp; }<br>}</p><p>You then use it like:</p><p>void Foo()<br>{<br>&nbsp; volatile bool keepDrawingAnimation = false;<br>&nbsp; var x = new SomeClass();<br>&nbsp; x.SomethingCompleted &#43;= new EventHandler( (o,e) =&gt; { keepDrawingAnimation = false; });<br>&nbsp; x.Start();<br>&nbsp; Console.WriteLine(&quot;Please wait...&quot;);<br>&nbsp; while(keepDrawingAnimation)<br>&nbsp; {<br>&nbsp; &nbsp; Console.Write(&quot;.&quot;); Thread,Sleep(100);<br>&nbsp; }<br>&nbsp; Console.WriteLine(&quot;Done&quot;);<br>}</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/27e3289e7b9e4417aca1a0be011498a4#27e3289e7b9e4417aca1a0be011498a4</link>
		<pubDate>Fri, 31 Aug 2012 16:47:03 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/27e3289e7b9e4417aca1a0be011498a4#27e3289e7b9e4417aca1a0be011498a4</guid>
		<dc:creator>evildictaitor</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/evildictaitor/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Question-on-C-Threads/27e3289e7b9e4417aca1a0be011498a4">1 minute&nbsp;ago</a>, <a href="/Niners/evildictaitor">evildictait​or</a> wrote</p><p>If you need it to be synchronous, don't use the thread. Threads should be for tasks going on in the background, not for doing critical stuff that you need to wait for.</p><p></p></div></blockquote><p></p><p>Oh not true sir. If I have 3 running tasks that are isolated from each other, but something needs the results of all 3 then that's a perfect use for threading.</p><p>&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/aa927a84757f4c0494d2a0be01156fff#aa927a84757f4c0494d2a0be01156fff</link>
		<pubDate>Fri, 31 Aug 2012 16:50:07 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/aa927a84757f4c0494d2a0be01156fff#aa927a84757f4c0494d2a0be01156fff</guid>
		<dc:creator>blowdart</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/blowdart/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>&nbsp;Thanks guys, it needs to be synchronous with respect to that one function, it should run in parallel with other threads before then. I wasn't too excited on using simply checking for null, because then I'm not sure if the thread ever ran at all.</p><p>I'll look into Tasks and see if it solves my problem. &nbsp;</p><p>Update 1</p><p>Not sure if tasks is what I need since this is an Event driven app. When a user does something I then try to Join the thread that should have been started a while ago and will most likely be finished.&nbsp;</p><p>Update 2</p><p>I feel a little more background would be of use. I have an app that currently loads information from a database when user selects an option from a drop down menu, there are few options, so I though I could pre-fetch the most used ones so there's no delay.&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/01e067480e804799bec3a0be0118a61f#01e067480e804799bec3a0be0118a61f</link>
		<pubDate>Fri, 31 Aug 2012 17:01:48 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/01e067480e804799bec3a0be0118a61f#01e067480e804799bec3a0be0118a61f</guid>
		<dc:creator>pavone</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/pavone/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Question-on-C-Threads/aa927a84757f4c0494d2a0be01156fff">32 minutes&nbsp;ago</a>, <a href="/Niners/blowdart">blowdart</a> wrote</p><p>*snip*</p><p>Oh not true sir. If I have 3 running tasks that are isolated from each other, but something needs the results of all 3 then that's a perfect use for threading.</p><p></p></div></blockquote><p></p><p>Perhaps I should have been more clear - I meant <em>Thread</em> the type, not <em>Threading&nbsp;</em>the concept.</p><p>System.Threading.Threads should be for tasks going on in the background.</p><p>If you're dealing with concurrent actions that you want to use in parallel, use something like Future&lt;T&gt; (or possibly BackgroundWorker).</p><p>Future&lt;int&gt; a = new Future&lt;T&gt;( () =&gt; Gen1 );<br>Future&lt;int&gt; b = new Future&lt;T&gt;( () =&gt; Gen2 );<br>Future&lt;int&gt; c = new Future&lt;T&gt;( () =&gt; Gen3 );<br>int d = Gen4();</p><p>return d &#43; a.Value &#43; b.Value &#43; c.Value;</p><p>&nbsp;</p><p>System.Threading.Thread shouldn't be used for anything other than a long-running parallel thread of execution (e.g. parsing a file in the background or handling incoming/outgoing network and pipe connections) - and in general you shouldn't need to Join with it to block the UI, you'll want to expose an event instead to let the UI remain smooth whilst doing the processing. System.Threading.Thread has a punishingly high startup cost. Don't use them for short running tasks or stuff that you're going to wait on. (Task and BackgroundWorker amortize the cost by using a threadpool, which is much better).</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/0f534835ffa84b52b0c9a0be011fbe5a#0f534835ffa84b52b0c9a0be011fbe5a</link>
		<pubDate>Fri, 31 Aug 2012 17:27:38 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/0f534835ffa84b52b0c9a0be011fbe5a#0f534835ffa84b52b0c9a0be011fbe5a</guid>
		<dc:creator>evildictaitor</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/evildictaitor/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>@<a href="/Forums/Coffeehouse/Question-on-C-Threads#c01e067480e804799bec3a0be0118a61f">pavone</a>: Why not check out the BackgroundWorker class? It's designed to fire off some task while keeping the UI thread free.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/75101cf094774813bec9a0be0120184c#75101cf094774813bec9a0be0120184c</link>
		<pubDate>Fri, 31 Aug 2012 17:28:55 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/75101cf094774813bec9a0be0120184c#75101cf094774813bec9a0be0120184c</guid>
		<dc:creator>Scott</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/spivonious/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Question-on-C-Threads/01e067480e804799bec3a0be0118a61f">46 minutes&nbsp;ago</a>, <a href="/Niners/pavone">pavone</a> wrote</p><p>&nbsp;Thanks guys, it needs to be synchronous with respect to that one function, it should run in parallel with other threads before then. I wasn't too excited on using simply checking for null, because then I'm not sure if the thread ever ran at all.</p><p>I'll look into Tasks and see if it solves my problem. &nbsp;</p><p>Update 1</p><p>Not sure if tasks is what I need since this is an Event driven app. When a user does something I then try to Join the thread that should have been started a while ago and will most likely be finished.&nbsp;</p><p>Update 2</p><p>I feel a little more background would be of use. I have an app that currently loads information from a database when user selects an option from a drop down menu, there are few options, so I though I could pre-fetch the most used ones so there's no delay.&nbsp;</p><p></p></div></blockquote><p></p><p>Ah ok. Well, sticking with Task you chould in your class have your</p><p>Task databaseLoader = new Task <em>blahblahblah</em></p><p>and start it running when you app starts.</p><p>Then inside your event you can check the databaseLoader.IsCompleted(), if it's not (and it's not faulted) then Task.WaitAll(databaseLoader);</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/ca72da2e14ae48849354a0be012656b1#ca72da2e14ae48849354a0be012656b1</link>
		<pubDate>Fri, 31 Aug 2012 17:51:39 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/ca72da2e14ae48849354a0be012656b1#ca72da2e14ae48849354a0be012656b1</guid>
		<dc:creator>blowdart</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/blowdart/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>Sounds good blowdart, except I'm limited to .NET Framework 3.5 and Task does not seem to be available in my .NET references...&nbsp;</p><p>I'm currently looking into BackgroundWorker, I've been reading a bit on it and it sounds pretty good. I wasn't too worried about Threads using up too many resources because I spawn very very few of them anyways, but it doesn't hurt to recycle.&nbsp;</p><p>You guys have been of great help though.&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/4aabfd1a56414298b842a0be0128bfc7#4aabfd1a56414298b842a0be0128bfc7</link>
		<pubDate>Fri, 31 Aug 2012 18:00:25 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/4aabfd1a56414298b842a0be0128bfc7#4aabfd1a56414298b842a0be0128bfc7</guid>
		<dc:creator>pavone</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/pavone/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>the dumb way is to have a static volatile int. You increment it by one before you start the thread. And in the thread, you decrement it by one at the end. And in your main thread, just brainlessly waiting the int to become 0.</p><p>Yupe, quite dumb, but, if you are not doing anything serious, the more dumb the better. <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-2.gif?v=c9' alt='Big Smile' /></p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/e087109712944b85aacda0be0131a647#e087109712944b85aacda0be0131a647</link>
		<pubDate>Fri, 31 Aug 2012 18:32:50 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/e087109712944b85aacda0be0131a647#e087109712944b85aacda0be0131a647</guid>
		<dc:creator>magicalclick</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/magicalclick/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>@<a href="/Forums/Coffeehouse/Question-on-C-Threads#ce087109712944b85aacda0be0131a647">magicalclick</a>: There are too many applications that start out as nothing serious, and then become serious, but still have the dumb code.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/aba5ff76f8bc48a0abe9a0be01349c85#aba5ff76f8bc48a0abe9a0be01349c85</link>
		<pubDate>Fri, 31 Aug 2012 18:43:37 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/aba5ff76f8bc48a0abe9a0be01349c85#aba5ff76f8bc48a0abe9a0be01349c85</guid>
		<dc:creator>kettch</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/kettch/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>@<a href="/Forums/Coffeehouse/Question-on-C-Threads#ce087109712944b85aacda0be0131a647">magicalclick</a>: And how do you wait for the int to become zero? Polling? Not to mention that addition/subtraction is not thread safe unless you're using interlocked operation. This method of doing it is not just dumb, but stupid.</p><p>If you want to go that route you can use WaitHandles, or a Semaphore, or in .Net 4.0 the CountdownEvent (which is a thread-safe way of doing what you described).</p><p>There's a simple rule with multi-threading: if you think you've found a simple way to do something, it's nearly always wrong (unless that simple way is using a class someone else wrote that encapsulates the hard stuff, like Task or BackgroundWorker).</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/0823857d6e5b4e08bfc2a0bf0028a544#0823857d6e5b4e08bfc2a0bf0028a544</link>
		<pubDate>Sat, 01 Sep 2012 02:27:59 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/0823857d6e5b4e08bfc2a0bf0028a544#0823857d6e5b4e08bfc2a0bf0028a544</guid>
		<dc:creator>Sven Groot</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Sven Groot/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Question-on-C-Threads/e087109712944b85aacda0be0131a647">8 hours&nbsp;ago</a>, <a href="/Niners/magicalclick">magicalclick</a> wrote</p><p>the dumb way is to have a static volatile int. You increment it by one before you start the thread. And in the thread, you decrement it by one at the end. And in your main thread, just brainlessly waiting the int to become 0.</p><p>Yupe, quite dumb, but, if you are not doing anything serious, the more dumb the better. <img src="http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-2.gif?v=c9" alt="Big Smile"></p><p></p></div></blockquote><p></p><p>It's harder to do that right (i.e. in a thread-safe way) than to just use a BackgroundWorker. Hence I would say that the BackgroundWorker is a better choice for beginners.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/7221f4d412f1412abdd2a0bf002e4913#7221f4d412f1412abdd2a0bf002e4913</link>
		<pubDate>Sat, 01 Sep 2012 02:48:31 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/7221f4d412f1412abdd2a0bf002e4913#7221f4d412f1412abdd2a0bf002e4913</guid>
		<dc:creator>evildictaitor</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/evildictaitor/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Question-on-C-Threads/0823857d6e5b4e08bfc2a0bf0028a544">3 hours&nbsp;ago</a>, <a href="/Niners/Sven%20Groot">Sven Groot</a> wrote</p><p>There's a simple rule with multi-threading: if you think you've found a simple way to do something, it's nearly always wrong (unless that simple way is using a class someone else wrote that encapsulates the hard stuff, like Task or BackgroundWorker).</p><p></p></div></blockquote><p></p><p>&#43;1</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/839fe4b3308541608cb2a0bf0069169f#839fe4b3308541608cb2a0bf0069169f</link>
		<pubDate>Sat, 01 Sep 2012 06:22:36 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/839fe4b3308541608cb2a0bf0069169f#839fe4b3308541608cb2a0bf0069169f</guid>
		<dc:creator>AndyC</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/AndyC/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>The OP's original question has not been answered. I think there is something going on there because if you first call foo() and then later bar(), there is no way that there could be a null ref exception in bar(). someThread will not be set to null automatically. We are not seeing all of the code so we can't answer that question.</p><p>Maybe someThread is really a static in the actual code and it is being set to null in the constructor? That would cause one instance to set it to null after another instance is using it. Just guessing since we don't have all of the code.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/22cca6540e7046bf94cca0bf01125194#22cca6540e7046bf94cca0bf01125194</link>
		<pubDate>Sat, 01 Sep 2012 16:38:45 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/22cca6540e7046bf94cca0bf01125194#22cca6540e7046bf94cca0bf01125194</guid>
		<dc:creator>BitFlipper</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/BitFlipper/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Question-on-C-Threads/22cca6540e7046bf94cca0bf01125194">14 minutes&nbsp;ago</a>, <a href="/Niners/BitFlipper">BitFlipper</a> wrote</p><p>We are not seeing all of the code so we can't answer that question.</p><p></p></div></blockquote><p></p><p>And therein lies the problem.</p><p>If someone asks a question about &quot;why does my code not work&quot; without providing at least an example that demonstrates the problem, the only option we have is to try psychic debugging to solve it.</p><p>If the OP wants it fixed, s/he needs to post an example that demonstrates the problem s/he's trying to solve.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/d34f97c83bda4aaa90c4a0bf0116b078#d34f97c83bda4aaa90c4a0bf0116b078</link>
		<pubDate>Sat, 01 Sep 2012 16:54:40 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/d34f97c83bda4aaa90c4a0bf0116b078#d34f97c83bda4aaa90c4a0bf0116b078</guid>
		<dc:creator>evildictaitor</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/evildictaitor/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>&nbsp;It's rather interesting, I tried to replicate the error in a console application and it worked just fine, no null reference, even though the logic is the same. Initially I thought it might have to do with scope. The code that's giving me the error is an ASP.NET webpage and the Thread logic is exactly as described in the first post.</p><p>In global scope, I have defined&nbsp; a thread and a static list (which is modified by such thread) and set thread to null and list to new List&lt;string&gt;().</p><p>I did a workaround by checking that the list has been changed, if not the it goes back to the old way of fetching after user request. Still, it intrigues me why that error is being thrown and I seem unable to replicate, it'll probably be something very simple I'm overlooking.</p><p>All the relevant code:</p><p><pre class="brush: csharp">protected void ddlRelTypeSelected(object sender, EventArgs e)
        {
            lblRelType.Visible = true;

            string strALPHA =&quot;&quot;;
           
            if (ddlPlatform.Text == &quot;IVWEX&quot;)
            {
                if (ddlType.Text == &quot;QA.Builds&quot; || ddlType.Text == &quot;QA.Releases&quot;)
                {
                    // ...
                }
                else
                {
                    // ...
                }
            }
            else if (ddlPlatform.Text == &quot;ALPHA&quot;)
            {
                if (ddlType.Text == &quot;QA.Candidates&quot;)
                {
                    // ...
                }
                else if (ddlType.Text == &quot;SCM.QA_Images&quot;)
                {
                    // ...
                }
                else    // QA.Releases
                {
                    strALPHA = ConfigurationManager.AppSettings[&quot;RelTypeAlpha&quot;].ToString();
                    string[] arrALPHA = strALPHA.Split(chsep);
                    ddlRelType.DataSource = arrALPHA;

                    // spawn osThread and gameThread
                    osThread = new Thread(() =&gt; getReleases(&quot;OS&quot;, alist));   
                    osThread.Start();
                    gamesThread = new Thread(() =&gt; getReleases(&quot;GAMES&quot;, blist));
                    gamesThread.Start();
                }
                ddlRelType.DataBind();
                ddlRelType.Visible = true;
            }     
        }

        private void getReleases(string SType, List&lt;string&gt; list)
        {
            OleDbConnection conn = openDBConnection(SCMSoftwareReleases);
            string SqlQuery = &quot; SELECT Release_Label as ReleaseLabel, Release_Type as ReleaseType &quot;
                      &#43; &quot; FROM TrackReleases &quot;
                      &#43; &quot; WHERE Platform = 'ALPHA' &quot;
                      &#43; &quot; AND Software_Type = '&quot; &#43; SType &#43; &quot;' &quot;
                      &#43; &quot; AND Release_Type &lt;&gt; 'Branch' &quot;
                      &#43; &quot; ORDER by 1 asc&quot;;
            OleDbDataAdapter da = new OleDbDataAdapter(SqlQuery, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataRowCollection drc = ds.Tables[&quot;Table&quot;].Rows;    // Table is default name
            list.Clear();
            
            for (int i = 0; i &lt; drc.Count; &#43;&#43;i)
                list.Add(drc[i][0].ToString() &#43; &quot;|&quot; &#43; drc[i][1].ToString());
            if (conn != null)
                conn.Close();
        }

        private void fetchReleases(ref string[] arr, string SType)
        {
            osThread.Join();    // null reference exception
            
            // ...
        }</pre></p><p>The webpage is a bunch of dropdown menus that leads to the sql query. The threads are without a doubt being spawned before fetchReleases is called because fetchReleases can only be called from a UI element that only appears after the call to ddlRelTypeSelected event handler.</p><p>Actually stepping through the debugger shows me that threads are finished before going to fetchReleases. <br>Now I'm thinking it's due to being a web app, the stateless nature of web apps has gotten me again?!</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/93d7f177147b4f94b0c7a0bf0154970f#93d7f177147b4f94b0c7a0bf0154970f</link>
		<pubDate>Sat, 01 Sep 2012 20:40:03 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/93d7f177147b4f94b0c7a0bf0154970f#93d7f177147b4f94b0c7a0bf0154970f</guid>
		<dc:creator>pavone</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/pavone/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>It's not clear that osThread is definitely assigned in all of the code blocks in ddRelTypeSelected. If any block doesn't assign it, osThread will still be null when you touch fetchReleases.</p><p>Additionally, it's a real shame that you can't use Future&lt;T&gt; (it's .NET), but here's a slightly less capable one that you might want to try out (warning: I haven't actually tested it)</p><p>class Future&lt;T&gt;<br>{<br>&nbsp; volatile T value;<br>&nbsp; volatile Exception e;<br>&nbsp; BackgroundWorker bgw;<br>&nbsp; AutoResetEvent done = new AutoResetEvent(false);<br>&nbsp; public Future(Func&lt;T&gt; generator)<br>&nbsp; {<br>&nbsp; &nbsp; bgw = new BackgroundWorker();<br>&nbsp; &nbsp; bgw.DoWork &#43;= (sender,e) =&gt; {<br>&nbsp; &nbsp; &nbsp; if(e.Cancel) return;<br>&nbsp; &nbsp; &nbsp; try {<br>&nbsp; &nbsp; &nbsp; &nbsp; value = generator();<br>&nbsp; &nbsp; &nbsp; } catch(Exception e) { this.e = e; }<br>&nbsp; &nbsp; &nbsp; } finally {&nbsp;<br>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; doneEvent.Set();<br>&nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; };<br>&nbsp; &nbsp; bgw.DoWorkAsync();<br>&nbsp; }<br>&nbsp; public T Value {<br>&nbsp; &nbsp; get {&nbsp;<br>&nbsp; &nbsp; &nbsp; done.WaitOne(); // wait for the thing to finish<br>&nbsp; &nbsp; &nbsp; if(e != null) throw e; // throw the exception synchronously if&nbsp;necessary<br>&nbsp; &nbsp; &nbsp; return value; // the value, as set by the background worker<br>&nbsp; &nbsp;}<br>&nbsp; }<br>}</p><p>Use like:</p><p>string[] fetchReleases()<br>{<br>&nbsp;&nbsp;Future&lt;List&lt;string&gt;&gt; pendingOses = new Future&lt;string&gt;( () =&gt; getReleases(&quot;OS&quot;) );<br>&nbsp;&nbsp;Future&lt;List&lt;string&gt;&gt; pendingGames = new Future&lt;string&gt;( () =&gt; getReleases(&quot;Games&quot;) );</p><p>&nbsp; List&lt;string&gt; games = pendingGames.Value;<br>&nbsp;&nbsp;List&lt;string&gt; oses = pendingOses.Value;</p><p>&nbsp; List&lt;string&gt; both = new List&lt;string&gt;();<br>&nbsp; both.AddRange(games);<br>&nbsp; both.AddRange(oses);</p><p>&nbsp; return both.ToArray();<br>}</p><p>(note, you'll need to change getReleases to return the List&lt;string&gt;).</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/d99e572717c34096b83da0bf01637ac1#d99e572717c34096b83da0bf01637ac1</link>
		<pubDate>Sat, 01 Sep 2012 21:34:15 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/d99e572717c34096b83da0bf01637ac1#d99e572717c34096b83da0bf01637ac1</guid>
		<dc:creator>evildictaitor</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/evildictaitor/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Question on C# Threads</title>
		<description><![CDATA[<p>&nbsp;You are correct, it's not assigned unless a set of specific menu items are selected from drop down menus, however I've made sure to make those selections and I know that it's been assigned by using the debugger. The thread runs, fills the list, and then is somehow set to null.</p><p>&nbsp;I'm very new to C# Threading, so I'm learning a lot through this project. Our website was initially done in ASP.NET Framework 2.0, I've changed it to 3.5 but the server does not support anything above that, so now I'm looking through the multiple ways of accomplishing this and best practices. Currently going through &quot;C# in a Nutshell&quot; which has been great. Your suggestions are much appreciated.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/436dbc2a490042adae8aa0bf016fd78b#436dbc2a490042adae8aa0bf016fd78b</link>
		<pubDate>Sat, 01 Sep 2012 22:19:16 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Question-on-C-Threads/436dbc2a490042adae8aa0bf016fd78b#436dbc2a490042adae8aa0bf016fd78b</guid>
		<dc:creator>pavone</dc:creator>
		<slash:comments>20</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/pavone/Discussions/RSS</wfw:commentRss>
	</item>
</channel>
</rss>