<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:evnet="http://www.mscommunities.com/rssmodule/"><channel><title>Comment Feed for Threading woes (TechOff on Channel 9)</title><atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/forums/techoff/416280-threading-woes/rss/default.aspx" /><image><url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url><title>Comment Feed for Threading woes (TechOff on Channel 9)</title><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/</link></image><description>Threading woes</description><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/</link><language>en-us</language><pubDate>Mon, 21 Jul 2008 14:34:27 GMT</pubDate><lastBuildDate>Mon, 21 Jul 2008 14:34:27 GMT</lastBuildDate><generator>EvNet (EvNet, Version=1.0.3243.35083, Culture=neutral, PublicKeyToken=null)</generator><item><title>Re: Re: Re: Threading woes</title><description>&lt;blockquote&gt;
				&lt;div&gt;phreaks said:&lt;/div&gt;
				&lt;div&gt;
						&lt;br /&gt;
						&lt;br /&gt;
						&lt;a title="WTF" href="http://weblogs.asp.net/justin_rogers/archive/2004/05/06/127580.aspx" target="_blank"&gt;I have also read that BeginRead isn't "really" asynchronous&lt;/a&gt;.&lt;br /&gt;&lt;/div&gt;
		&lt;/blockquote&gt;
		&lt;br /&gt;From the comments of that blog post, it seems that it's a CLR 1.x problem. Plus there's a work-around by checking the CompletedSynchronously prop to avoid a stack overflow.&lt;br /&gt;&lt;br /&gt;What framework are you on?&lt;br /&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416749</link><pubDate>Mon, 21 Jul 2008 14:34:27 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416749</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416749/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	phreaks said:
				
						
						
						I have also read that BeginRead isn't "really" asynchronous.
		
		From the comments of that blog post, it seems that it's a CLR 1.x problem. Plus there's a work-around by checking the CompletedSynchronously prop to avoid a stack overflow.What framework are you on?</evnet:previewtext><dc:creator>Minh</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416749/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Re: Threading woes</title><description>&lt;p&gt;Ok you need a "watchdog" that can re-start the tcp connection, that should be ok.&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;and I would still spin off a worker to read the data, and fire some kind of "got data" event.&lt;/p&gt;
&lt;p&gt;your DoWOrk can run the watchdog and if no datarecived in xx time then do a re-set and re-start of the tcp connection.&lt;br /&gt;&lt;br /&gt;service control is handled the way I outlined before.&lt;/p&gt;
&lt;p&gt;just that you get the read/write/ TCP/IO on a seperate thread .... Hmmm perhaps 2 bgw processes?? one for TCP and the other for SCM??&lt;/p&gt;
&lt;p&gt;that way your TCP data can be read and processed asnyc from any SCM state.&lt;/p&gt;
&lt;p&gt;but the SCM worker can tell the TCP worker to stop /restart/exit.&lt;/p&gt;
&lt;p&gt;I do not have my TCP/Networking book handy at home to check on the details.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416639</link><pubDate>Mon, 21 Jul 2008 01:08:03 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416639</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416639/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Ok you need a "watchdog" that can re-start the tcp connection, that should be ok.
and I would still spin off a worker to read the data, and fire some kind of "got data" event.
your DoWOrk can run the watchdog and if no datarecived in xx time then do a re-set and re-start of the tcp&amp;#8230;</evnet:previewtext><dc:creator>figuerres</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416639/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Threading woes</title><description>Yeah, you mostly understand what I need, but there is one small caveat.&lt;br /&gt;&lt;br /&gt;I am not spinning up new TCP connections, doing some work and then killing the connection.&lt;br /&gt;&lt;br /&gt;I am spinning up a persistant connection to a server that will generally be receiving data perpetually. However, there can and will be periods when the server may not transmit any data for minutes or possibly even an hour.&lt;br /&gt;&lt;br /&gt;I have to authenticate with the server, and it only allows 1 active connection with my credentials.&lt;br /&gt;&lt;br /&gt;So I need to handle &lt;br /&gt;1) When the service receives a stop/reset/pause command&lt;br /&gt;2) When the there has been no activity from the server for x period, shutdown the socket for a few minutes (the server will que any data I mised for an hour) and start listening again.&lt;br /&gt;&lt;br /&gt;The real problem is that sometimes there is an error on the server and it doesn't abort the connection, it just stops sending data. So I want to reset the TCP connection every hour.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Do you think your approach will still work?</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416619</link><pubDate>Sun, 20 Jul 2008 23:06:14 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416619</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416619/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Yeah, you mostly understand what I need, but there is one small caveat.I am not spinning up new TCP connections, doing some work and then killing the connection.I am spinning up a persistant connection to a server that will generally be receiving data perpetually. However, there can and will be&amp;#8230;</evnet:previewtext><dc:creator>phreaks</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416619/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Threading woes</title><description>Not sure If I am following this so let me know if I have it wrong.&lt;br /&gt;&lt;br /&gt;you are needing to read some data from a remote server yes?&lt;br /&gt;&lt;br /&gt;so then you should setup the request to be serviced on an async worker that when it completes will notfiy your serive that the request has been completed&lt;br /&gt;&lt;br /&gt;your DoWOrk should kick off the worker and then go back to a sleep loop.&lt;br /&gt;&lt;br /&gt;when it wakes up if it then needs to do something it does.&lt;br /&gt;&lt;br /&gt;if your worker can process the data it does that before it dies if it's a one shot call.&lt;br /&gt;then your service can get a stop command from scm and if it has to it can terminate the workers as needed.&lt;br /&gt;&lt;br /&gt;you do not start a long runing / blocking task on your main service loop.&lt;br /&gt;&lt;br /&gt;if you look at code for unix deamons for example you will see they do a set of fork and spawn commands to branch off into multiple thread / process /task depdneding on which version of UNIx / Linux/&amp;nbsp; BSD etc... they are.&lt;br /&gt;&lt;br /&gt;sounds kind of sick but the parents sit around waiting for the children to die.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416610</link><pubDate>Sun, 20 Jul 2008 22:27:13 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416610</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416610/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Not sure If I am following this so let me know if I have it wrong.you are needing to read some data from a remote server yes?so then you should setup the request to be serviced on an async worker that when it completes will notfiy your serive that the request has been completedyour DoWOrk should&amp;#8230;</evnet:previewtext><dc:creator>figuerres</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416610/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Threading woes</title><description>What do you mean?&lt;br /&gt;&lt;br /&gt;Is there an easier way?&lt;br /&gt;&lt;br /&gt;PS.. &lt;br /&gt;You're avatar is broken on every other load.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416591</link><pubDate>Sun, 20 Jul 2008 21:17:54 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416591</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416591/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>What do you mean?Is there an easier way?PS.. You're avatar is broken on every other load.</evnet:previewtext><dc:creator>phreaks</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416591/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Threading woes</title><description>Yes the read probably&amp;nbsp;is blocking and causing the problem, but I wondered why you were recreating the wheel really..</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416584</link><pubDate>Sun, 20 Jul 2008 20:57:34 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416584</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416584/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Yes the read probably&amp;nbsp;is blocking and causing the problem, but I wondered why you were recreating the wheel really..</evnet:previewtext><dc:creator>stevo_</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416584/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Threading woes</title><description>Dexter is right.&lt;br /&gt;&lt;br /&gt;BackgroundWorker isn't going to work for me because it is the read method that's blocking, and that is where my headache starts.&lt;br /&gt;&lt;br /&gt;The Server will only allow 1 connection, so I have to be very careful how I close and initiate a connection.&lt;br /&gt;&lt;br /&gt;I have reservations though with using BeginRead because as I understand it, I can't be guaranteed the order that the stream is read, which means I can't save it in the correct order.&lt;br /&gt;&lt;br /&gt;&lt;a title="WTF" href="http://weblogs.asp.net/justin_rogers/archive/2004/05/06/127580.aspx" target="_blank"&gt;I have also read that BeginRead isn't "really" asynchronous&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I think I am stuck.&lt;br /&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416574</link><pubDate>Sun, 20 Jul 2008 20:30:50 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416574</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416574/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Dexter is right.BackgroundWorker isn't going to work for me because it is the read method that's blocking, and that is where my headache starts.The Server will only allow 1 connection, so I have to be very careful how I close and initiate a connection.I have reservations though with using BeginRead&amp;#8230;</evnet:previewtext><dc:creator>phreaks</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416574/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Threading woes</title><description>Sweet dude.&lt;br /&gt;&lt;br /&gt;I will try this.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416371</link><pubDate>Sat, 19 Jul 2008 21:37:33 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416371</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416371/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Sweet dude.I will try this.</evnet:previewtext><dc:creator>phreaks</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416371/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Threading woes</title><description>&lt;p&gt;Guys, I have used BGW&amp;nbsp; in multiple service projects and it rocks!&amp;nbsp; works super sweet!!&lt;/p&gt;
&lt;p&gt;in general what I do is this:&lt;/p&gt;
&lt;p&gt;OnStart calls RunWOrkerAsync&lt;br /&gt;OnStop / ReStart set the cancel flag of the BW&lt;br /&gt;the DoWork gets a setup like this:&lt;/p&gt;
&lt;p&gt;While(&amp;nbsp; !CancelFlag &amp;amp;&amp;amp; !ExitFlag){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dosomething();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Sleep(timeout);&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;where the sleep time needs to be less than 30 seconds to work with scm and respond in time.&lt;/p&gt;
&lt;p&gt;the "dosomething()"&amp;nbsp; is the part that needs to be custom for each service and may test and exit fast if nothing needs to be done.&lt;br /&gt;as you are doing tcp/ip you may want to also setup a thread pool also to do the actual netowork stuff.&lt;br /&gt;keep the DoWOrk()&amp;nbsp; as a main controller and dispatcher &lt;/p&gt;
&lt;p&gt;so for example it listens and when a new message / connection happens you spin up a new worker thread to handle it. that way the main worker / DoWOrk keeps ready to handle more dispatching of work.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416340</link><pubDate>Sat, 19 Jul 2008 21:23:29 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416340</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416340/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Guys, I have used BGW&amp;nbsp; in multiple service projects and it rocks!&amp;nbsp; works super sweet!!
in general what I do is this:
OnStart calls RunWOrkerAsyncOnStop / ReStart set the cancel flag of the BWthe DoWork gets a setup like this:
While(&amp;nbsp; !CancelFlag &amp;amp;&amp;amp;&amp;#8230;</evnet:previewtext><dc:creator>figuerres</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416340/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Threading woes</title><description>Thanks.&lt;br /&gt;&lt;br /&gt;This is very helpful.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416303</link><pubDate>Sat, 19 Jul 2008 18:08:19 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416303</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416303/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Thanks.This is very helpful.</evnet:previewtext><dc:creator>phreaks</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416303/Trackback.aspx</trackback:ping></item><item><title>Re: Threading woes</title><description>I'd be a bit careful about using BackgroundWorker in a service application. While it works its behavior is different from the one you get in a WinForms application, for example its ReportProgress notification will go on a thread from the thread pool rather than the main thread.&lt;br /&gt;&lt;br /&gt;In any case, it still relies on the worker thread checking a value to see if "cancel" was request (CancellationPending property) and we're back to square one. The most likely reason why it takes a while for that thread to stop is NetworkStream.Read. Read is a blocking call, if there's no data available it just sits there waiting so your _stopRequested variable will only be checked when the server sends something.&lt;br /&gt;&lt;br /&gt;A simple workaround would be to set a reasonable ReadTimeout, 100 ms for example.&lt;br /&gt;A brutal aproach would be to either abort the worker thread or close the socket from the main thread.&lt;br /&gt;A more complex solution would&amp;nbsp; be go async by using BeginRead instead of Read which avoid the blocking.&lt;br /&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416300</link><pubDate>Sat, 19 Jul 2008 17:45:58 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416300</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416300/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>I'd be a bit careful about using BackgroundWorker in a service application. While it works its behavior is different from the one you get in a WinForms application, for example its ReportProgress notification will go on a thread from the thread pool rather than the main thread.In any case, it still&amp;#8230;</evnet:previewtext><dc:creator>Dexter</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416300/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Threading woes</title><description>Thanks, I'll check it out.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416295</link><pubDate>Sat, 19 Jul 2008 17:01:43 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416295</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416295/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Thanks, I'll check it out.</evnet:previewtext><dc:creator>phreaks</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416295/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Threading woes</title><description>Its often used for UI to keep the UI thread ready, but I think the technical design for it is to help with the problem you have above.. it wraps a thread and provides built in functionality for calling back the complete method on the right thread, and being able to push cancellations in from other threads, yadda yadda.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416289</link><pubDate>Sat, 19 Jul 2008 16:36:31 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416289</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416289/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Its often used for UI to keep the UI thread ready, but I think the technical design for it is to help with the problem you have above.. it wraps a thread and provides built in functionality for calling back the complete method on the right thread, and being able to push cancellations in from other threads, yadda yadda.</evnet:previewtext><dc:creator>stevo_</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416289/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Threading woes</title><description>Well, honestly, I dunno. I've never used a background worker.&lt;br /&gt;For some reason I thought that was for synching with a UI, and my service has no UI.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416285</link><pubDate>Sat, 19 Jul 2008 16:20:00 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416285</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416285/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Well, honestly, I dunno. I've never used a background worker.For some reason I thought that was for synching with a UI, and my service has no UI.</evnet:previewtext><dc:creator>phreaks</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416285/Trackback.aspx</trackback:ping></item><item><title>Re: Threading woes</title><description>Just quickly, before I analyse your code, why aren't you using BackgroundWorker? it handles the thread sync for calling back completion and handles doing pending&amp;nbsp;cancellations etc.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416283</link><pubDate>Sat, 19 Jul 2008 16:10:24 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/416280-Threading-woes/?CommentID=416283</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/416283/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Just quickly, before I analyse your code, why aren't you using BackgroundWorker? it handles the thread sync for calling back completion and handles doing pending&amp;nbsp;cancellations etc.</evnet:previewtext><dc:creator>stevo_</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/416283/Trackback.aspx</trackback:ping></item></channel></rss>