<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx</link><description>Thread.Sleep has it&amp;#39;s use: simulating lengthy operations while testing/debugging on an MTA thread. In .NET there&amp;#39;s no other reason to use it. Thread.Sleep(n) means block the current thread for at least the number of timeslices (or thread quantums</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1739735</link><pubDate>Sat, 14 Nov 2009 13:56:21 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1739735</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@Richard. &amp;nbsp;Right, using Thread.Sleep(1) will allow any other thread to take control of the CPU for at least a time-slice.&lt;/p&gt;
&lt;p&gt;The question is a little misguided. &amp;nbsp;The parameter to Sleep is the number of milliseconds to sleep. &amp;nbsp;The fact that we&amp;#39;re using 1 millisecond as a parameter means it&amp;#39;s *really* difficult for any number of threads to be calling Thread.Sleep at the very same time. &amp;nbsp;Then, there&amp;#39;s the fact that only one thread at a time per processor is actually running...&lt;/p&gt;
&lt;p&gt;Your question suggests to me that you want to dip into thread prioritization. &amp;nbsp;You want to periodically do no work so other applications can do some. &amp;nbsp;But, you don&amp;#39;t know what those other applications do or whether stopping your work will benefit them at all. &amp;nbsp;I wouldn&amp;#39;t suggest writing your application to do this; there&amp;#39;s no way to do it correctly without being the operating system. &amp;nbsp;Using Thread.Sleep(1) in your threads is enough for the operating system to allow other threads to be responsive. &amp;nbsp;It works remarkably well.&lt;/p&gt;
&lt;p&gt;Are you using Thread.Sleep(1) and seeing that other applications are being starved of CPU?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1739735" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1739682</link><pubDate>Sat, 14 Nov 2009 08:51:32 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1739682</guid><dc:creator>Richard</dc:creator><description>&lt;p&gt;We are looking for a way to relinquish CPU cycles to other applications in a milti-threaded application. If we use sleep.thread(1) in each thread then my assumption is that our other threads will take the CPU.&lt;/p&gt;
&lt;p&gt;The question is...how can we use thread.sleep(1) on all of our threads simultaneously so that CPU cycles are open for other applications?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1739682" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1730988</link><pubDate>Fri, 09 Oct 2009 08:30:22 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1730988</guid><dc:creator>Jimbo</dc:creator><description>&lt;p&gt;How would you handle email throttling in a windows service?&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1730988" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1713273</link><pubDate>Wed, 05 Aug 2009 03:28:11 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1713273</guid><dc:creator>Josh</dc:creator><description>&lt;p&gt;Wow, this is a really hot topic. I must admit to not fully understanding all the intricacies of STA and MTA threads, but the discussion itself is quite enthralling. Thanks for taking the time to explain it in such detail. Now just to wrap it around my brain, lol.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1713273" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1699058</link><pubDate>Thu, 09 Jul 2009 18:09:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1699058</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;I wrote a version of CPUUsage to acquire the CPU% using a timer. &amp;nbsp;Is this what you mean by a good example of “Sleepless” coding?&lt;/p&gt;
&lt;p&gt;Public Class Form1&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Dim CPU_Percentage As New CPUUsage&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TextBox1.Text = CPU_Percentage.GetCurrentCPUActivity &amp;amp; &amp;quot;%&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;End Sub&lt;/p&gt;
&lt;p&gt;End Class&lt;/p&gt;
&lt;p&gt;Public Class CPUUsage&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Private ReadOnly PCPUUsage As PerformanceCounter&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Public Sub New()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PCPUUsage = New PerformanceCounter&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PCPUUsage.CategoryName = &amp;quot;Processor&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PCPUUsage.CounterName = &amp;quot;% Processor Time&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PCPUUsage.InstanceName = &amp;quot;_Total&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PCPUUsage.NextValue()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;End Sub&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Public Function GetCurrentCPUActivity() As Integer&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Return PCPUUsage.NextValue()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;End Function&lt;/p&gt;
&lt;p&gt;End Class&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1699058" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1699018</link><pubDate>Thu, 09 Jul 2009 15:24:54 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1699018</guid><dc:creator>MikeB</dc:creator><description>&lt;p&gt;Just as a foolowup - &lt;/p&gt;
&lt;p&gt;Many of my tasks don&amp;#39;t have to do anything for roughly 5 seconds. I use tickcount to measure that time in ms. But I don&amp;#39;t need to be looking at that tick count every cycle. If the task works in 4.95 seconds as opposed to 5.001, I really don&amp;#39;t care. If it takes slightly longer, I really don&amp;#39;t care either. So, if I sleep for 100 ms before looking st tick counts, I have one line of code for someone else to follow that gets my CPU usage back down to normal.&lt;/p&gt;
&lt;p&gt;Obviously, I could put a bunch of code in there to basically accomplish the same thing to make thread behave in a way YOU seem to need - not me.&lt;/p&gt;
&lt;p&gt;So, my point is, you&amp;#39;ve painted with a pretty broad brush here and I just don&amp;#39;t think your argument holds water in every case. I beleive that the sleep command DOES have a place in non-critical timing threads. Part of my job is not just getting systems to work, it&amp;#39;s also making it easy for someone else to take over the code.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve been writing realtime systems for a lot of years, and not once - ever - has the sleep command come back to bite me. Sometimes we don&amp;#39;t need a sledgehammer when a tack hammer will do. It&amp;#39;s not poor design - it&amp;#39;s simplicity.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1699018" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1699013</link><pubDate>Thu, 09 Jul 2009 15:10:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1699013</guid><dc:creator>MikeB</dc:creator><description>&lt;p&gt;I know I&amp;#39;m not as smart as the author or the posters here, but I use Thread.Sleep all the time.&lt;/p&gt;
&lt;p&gt;I write software for realtime systems. I&amp;#39;m continually polling I/O and running state machines. If I do NOT use the sleep command when polling (and this goes for all the threads in the application - could be anywhere from 2 to 15), CPU usage goes up over 90%. If I sleep for 100 ms, it goes back close to zero.&lt;/p&gt;
&lt;p&gt;I usually have other applications running on the system as well and I don&amp;#39;t want to hang them up. What other method exists to run realtime loops without penalizing other applications on the same machine?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1699013" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1698818</link><pubDate>Wed, 08 Jul 2009 20:37:28 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698818</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;I wrote my post not in order to get information on the Sleep method but in the hopes to find a solution to the high CPU% that would allow me to eliminate the Sleep. &amp;nbsp;I searched to find out how to acquire the CPU%, much like is shown in Task Manager, and came across the following MSDN page:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx"&gt;msdn.microsoft.com/.../system.diagnostics.performancecounter.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;What are your thoughts on examples on the page which use the Sleep method, especially the CPUUsage class in the community comment section?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698818" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1698809</link><pubDate>Wed, 08 Jul 2009 19:57:36 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698809</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@Andy. I think it&amp;#39;s a bad design because you&amp;#39;re committing your thread for a specific amount of time, it can&amp;#39;t be interrupted and you can&amp;#39;t safely stop it until it stops sleeping.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698809" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1698543</link><pubDate>Tue, 07 Jul 2009 05:14:46 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698543</guid><dc:creator>Andy</dc:creator><description>&lt;p&gt;I use thread-sleep to slow-down my CPU/IO intensive non-critical background tasks apps (on a server) to make it more resource friendly.&lt;/p&gt;
&lt;p&gt;IS that a bad design?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698543" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1697405</link><pubDate>Tue, 30 Jun 2009 22:05:48 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697405</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@Dave: The reason you see a difference between Sleep(1) and Sleep(5) (or anything &amp;nbsp;below Sleep(100) is that the amount of time you give to Sleep is a recommendation, the system causes that thread to relinquish control of the the processor for a number of timeslices that fit within the requested delay time. &amp;nbsp;Time slices are 100-200 ms (depending on the version of Windows) and therefore anything above 1 would effectively be 100/200.&lt;/p&gt;
&lt;p&gt;1 and 0 have special meaning. &amp;nbsp;0 means relinquish control only if another thread of equal priority (not equal and greater) is waiting and 1 means only relinquish control if any other thread that is waiting. &amp;nbsp;Sleep(0) is not recommended because it doesn&amp;#39;t do what you expect--you should always use Sleep(1) to relinquish control to other threads.&lt;/p&gt;
&lt;p&gt;Sleep(1) is a generally acceptable means of relinquishing control to other threads; but it&amp;#39;s generally better to design it so it doesn&amp;#39;t hog the CPU.&lt;/p&gt;
&lt;p&gt;In your case, if and I were fixing it, I would move towards using a scheduling system where there is no &amp;quot;waiting&amp;quot; required--i.e. the scheduler (like Quartz.NET) is responsible for queueing up the reports to run). &amp;nbsp;But, it&amp;#39;s hard to recommend something specific without more thorough knowledge of the solution.&lt;/p&gt;
&lt;p&gt;I really need another post that details Sleep(0) and Sleep(1)...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1697405" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1697402</link><pubDate>Tue, 30 Jun 2009 20:58:09 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697402</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;Back in 2003 we ported a massive Windows application to ASP.NET. &amp;nbsp;The application has 200 Crystal reports with user selectable parameters (facilities, areas, items, date range, etc…) so that each report may take up to several minutes to run depending on the report, the data, and the parameters selected. &amp;nbsp;Each customer has their own copy of the database. &amp;nbsp;We store Session State in SQL Server, which includes the datasets for the reports. &amp;nbsp;As we migrated more customers to the web app we ran into a problem in late 2007 where SQL Server would cause the CPU to hit 100% and the application would become unresponsive. &amp;nbsp;As a quick workaround solution we added a Sleep(1) to our library function that executes each query. &amp;nbsp;Some reports execute a few queries while others may execute thousands. &amp;nbsp;This temporarily solved the problem but of course had the negative side affect of slowing the reports down. &amp;nbsp;As we continued to add customers to the site we had to eventually bump up to Sleep(5) over the course of two months. &amp;nbsp;We spent weeks performance tuning a number of slow running reports and have not had any problems for the past year even though we now have thousands of users. &amp;nbsp;We tried lowering the Sleep last summer but the 100% CPU problem came back again. &amp;nbsp;What would you suggest I do to eliminate the sleep so that the reports run faster but without SQL Server maxing out the CPU?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1697402" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1697367</link><pubDate>Tue, 30 Jun 2009 16:54:30 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697367</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@bo. &amp;nbsp;The fact that it functions better with a call to Sleep(0) means it wasn&amp;#39;t designed properly. &amp;nbsp;Sleep(0) (while not as flawed as Sleep(x)) is like goto...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1697367" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1697359</link><pubDate>Tue, 30 Jun 2009 16:16:05 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697359</guid><dc:creator>Bo</dc:creator><description>&lt;p&gt;Back in the day when i played Everquest, that particular game had poor coding and it was very unresponsive especially if you were playing multiple accounts on the same computer and wanted to Alt-Tab between the different game instances..&lt;/p&gt;
&lt;p&gt;As an example when you had one of your characters auto-following the other character, the character whose gameprocess did not have window focus, would fall behind because it didnt get enough time-slices to actually do all the processing it needed to do to (literally) keep up when you were running around with your characters.&lt;/p&gt;
&lt;p&gt;Fortunatly it was possible by code injection to hook into the process and a simple Sleep(0) injected into the end of the game loop of each process made a huge difference in playability. &lt;/p&gt;
&lt;p&gt;Suddenly it was possible to play multiple characters on the same computer, because now &amp;nbsp;the available CPU ressources got distributed equally and in a fair manner between all game instances.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not even sure who I blame the need for this hack on, the Everquest programmers, or the guys who wrote the windows scheduler..&lt;/p&gt;
&lt;p&gt;I&amp;#39;m actually tending towards the latter. &lt;/p&gt;
&lt;p&gt;But at least this is one case where Sleep(0) actually made a huge difference(the difference between being able to control 4 characters on one computer as opposed to max 2)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1697359" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1696807</link><pubDate>Fri, 26 Jun 2009 17:42:44 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1696807</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@Niki: Using Thread.Sleep(1) will relinquish the CPU to another waiting thread; which is an acceptable use of Sleep. &amp;nbsp;What happens with Sleep(10) is that it&amp;#39;s lower than the thread quantum (which is 100-200 ms, depending on the type of the OS), which really ends up being Sleep(1).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1696807" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1696805</link><pubDate>Fri, 26 Jun 2009 16:38:12 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1696805</guid><dc:creator>SanjayU</dc:creator><description>&lt;p&gt;I&amp;#39;ll admit I&amp;#39;ve used it, but it does make me feel a little dirty.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1696805" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1696782</link><pubDate>Fri, 26 Jun 2009 11:20:18 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1696782</guid><dc:creator>Niki</dc:creator><description>&lt;p&gt;I commonly use Thread.Sleep for tasks like this: I call (e.g.) Socket.Connect, but it fails because the Server is not running yet, so I have to retry connecting until the server is up. If I retry connecting right away, my connect thread will under certain conditions use 100% CPU. To prevent this, I simply call Thread.Sleep(10) after each failed attempt to connect to the server.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1696782" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1696709</link><pubDate>Thu, 25 Jun 2009 13:23:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1696709</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@deman: If you use System.Timers or Threading.Timers, those end up being native too. &amp;nbsp;It doesn&amp;#39;t defeat the purpose, it only gets into permission issues with assemblies that call native APIs (the .NET Framework assemblies have native permissions by default).&lt;/p&gt;
&lt;p&gt;The timers are meant as timers; it&amp;#39;s highly unlikely that a &amp;quot;timer&amp;quot; would need to run pass 49 days, which is why it doesn&amp;#39;t support an infinite amount of time.&lt;/p&gt;
&lt;p&gt;Beyond 49 days (well, beyond a day; but that&amp;#39;s beside the point) you&amp;#39;re really looking at scheduling actions to run, not a timer. &amp;nbsp;There&amp;#39;s libraries like Quartz.NET (&lt;a rel="nofollow" target="_new" href="http://quartznet.sourceforge.net/"&gt;quartznet.sourceforge.net&lt;/a&gt;) for scheduling tasks.&lt;/p&gt;
&lt;p&gt;So, the only other option with a timer is to chain timers. &amp;nbsp;Set a timer for the maximum value (49+days, depending on the timer, and when that expires/elapses, start another timer--keep doing that until the final amount of time has elapsed.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1696709" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1696668</link><pubDate>Thu, 25 Jun 2009 02:32:26 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1696668</guid><dc:creator>deman</dc:creator><description>&lt;p&gt;I can&amp;#39;t use anything native it would defeat the purpose of my project. Bad enough that native limitations present themselves so crudely in .NET...&lt;/p&gt;
&lt;p&gt;Every timer that uses current time as a basis is susceptible to user or automated date-time updates messing up any timing. Unless the OS updates waitable timers on every adjustment... DOES IT?&lt;/p&gt;
&lt;p&gt;It&amp;#39;s clear that some kind of nonadjustable counter is needed for such timings like the hardware performance counter or worst case the TickCount which is interrupt driven.&lt;/p&gt;
&lt;p&gt;SetTimer() could have been written to work properly since there is an USER_TIMER_MAXIMUM with the value of 0x7fffffff given this one could actually handle a wraparound, I could even write the C code for it, but DID THEY?&lt;/p&gt;
&lt;p&gt;As far as I can see from posts and writings on the net there are reliability problems with every one currently available timer. Which is insane given that it&amp;#39;s a very very basic and common task.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1696668" width="1" height="1"&gt;</description></item><item><title>re: Thread.Sleep is a sign of a poorly designed program.</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx#1696666</link><pubDate>Thu, 25 Jun 2009 00:22:36 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1696666</guid><dc:creator>PeterRitchie</dc:creator><description>&lt;p&gt;@deman: I would use the native waitable timers if you want to support an interval greater than 49 days. &amp;nbsp;The waitable timers support dates (UTC) so you effectively can have years between intervals. &amp;nbsp;Start with &lt;a rel="nofollow" target="_new" href="http://www.pinvoke.net/default.aspx/kernel32/SetWaitableTimer.html"&gt;www.pinvoke.net/.../SetWaitableTimer.html&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1696666" width="1" height="1"&gt;</description></item></channel></rss>