Click here to Skip to main content
       

C#

 
GeneralRe: C# run a process Pinmemberclassy_dog19-Feb-13 5:38 
GeneralRe: C# run a process PinmvpEddy Vluggen19-Feb-13 9:34 
QuestionThread.Sleep is NOT evil Pinmemberdevvvy18-Feb-13 5:55 
AnswerRe: Thread.Sleep is NOT evil PinprotectorPete O'Hanlon18-Feb-13 6:15 
devvvy wrote:
Thread.Sleep is one-line of code, simple, and get the job done

Just because it's simple to implement, this doesn't mean it's a good choice. I recommend against Thread.Sleep precisely because of what it does - it suspends a thread for a timeout period. That doesn't seem too bad if you want to wait for a period, but the big problem comes when you want to terminate that thread. When a thread is sleeping, terminating it isn't easy - effectively, you have to wait for the thread to wake up just to kill it. This may not seem to be too big a deal, but if your system relies on having a number of these internally, you have just forced the user to wait and that's generally not a good thing.
 
The primary reason that I recommend against it is precisely because it's so easy to use. The problem is, people end up using it as a blunt object and force it into programs that it shouldn't be in, precisely because they don't understand the downsides. If you fully appreciate the tradeoffs in Thread.Sleep then, by all means, use it. Don't, however, put it into code that a noob is going to read - as they probably don't appreciate the underlying issues with Thread.Sleep then they will blindly use it.
 
There are other, subtle, problems with the use of Thread.Sleep, with relation to priority. A simple pattern, though, is simply to use Monitor.Enter to control the access, and this is my preferred choice in a lot of cases.
I was brought up to respect my elders. I don't respect many people nowadays.

CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

GeneralRe: Thread.Sleep is NOT evil Pinmemberdevvvy18-Feb-13 13:29 
GeneralRe: Thread.Sleep is NOT evil [modified] PinmemberN a v a n e e t h18-Feb-13 16:12 
GeneralTHANK YOU - this is the answer I was looking for. Pinmemberdevvvy19-Feb-13 12:53 
GeneralRe: Thread.Sleep is NOT evil PinprotectorPete O'Hanlon18-Feb-13 20:43 
GeneralApplication Exit vs Thread.Abort? Pinmemberdevvvy19-Feb-13 13:12 
GeneralRe: Thread.Sleep is NOT evil PinmemberSimon_Whale18-Feb-13 13:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 20 Apr 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid