Click here to Skip to main content
15,881,882 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# run a process Pin
classy_dog18-Feb-13 8:22
classy_dog18-Feb-13 8:22 
GeneralRe: C# run a process Pin
Eddy Vluggen18-Feb-13 8:30
professionalEddy Vluggen18-Feb-13 8:30 
GeneralRe: C# run a process Pin
classy_dog18-Feb-13 14:04
classy_dog18-Feb-13 14:04 
GeneralRe: C# run a process Pin
Eddy Vluggen18-Feb-13 22:31
professionalEddy Vluggen18-Feb-13 22:31 
GeneralRe: C# run a process Pin
classy_dog19-Feb-13 5:38
classy_dog19-Feb-13 5:38 
GeneralRe: C# run a process Pin
Eddy Vluggen19-Feb-13 9:34
professionalEddy Vluggen19-Feb-13 9:34 
QuestionThread.Sleep is NOT evil Pin
devvvy18-Feb-13 5:55
devvvy18-Feb-13 5:55 
AnswerRe: Thread.Sleep is NOT evil Pin
Pete O'Hanlon18-Feb-13 6:15
mvePete 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 Pin
devvvy18-Feb-13 13:29
devvvy18-Feb-13 13:29 
GeneralRe: Thread.Sleep is NOT evil Pin
N a v a n e e t h18-Feb-13 16:12
N a v a n e e t h18-Feb-13 16:12 
GeneralTHANK YOU - this is the answer I was looking for. Pin
devvvy19-Feb-13 12:53
devvvy19-Feb-13 12:53 
GeneralRe: Thread.Sleep is NOT evil Pin
Pete O'Hanlon18-Feb-13 20:43
mvePete O'Hanlon18-Feb-13 20:43 
GeneralApplication Exit vs Thread.Abort? Pin
devvvy19-Feb-13 13:12
devvvy19-Feb-13 13:12 
GeneralRe: Thread.Sleep is NOT evil Pin
Simon_Whale18-Feb-13 13:41
Simon_Whale18-Feb-13 13:41 
GeneralRe: Thread.Sleep is NOT evil Pin
N a v a n e e t h18-Feb-13 16:15
N a v a n e e t h18-Feb-13 16:15 
GeneralRe: Thread.Sleep is NOT evil Pin
devvvy19-Feb-13 1:41
devvvy19-Feb-13 1:41 
GeneralRe: Thread.Sleep is NOT evil Pin
Pete O'Hanlon19-Feb-13 2:18
mvePete O'Hanlon19-Feb-13 2:18 
GeneralRe: Thread.Sleep is NOT evil Pin
N a v a n e e t h19-Feb-13 6:33
N a v a n e e t h19-Feb-13 6:33 
GeneralRe: Thread.Sleep is NOT evil Pin
devvvy19-Feb-13 12:47
devvvy19-Feb-13 12:47 
GeneralRe: Thread.Sleep is NOT evil Pin
devvvy19-Feb-13 12:35
devvvy19-Feb-13 12:35 
GeneralRe: Thread.Sleep is NOT evil Pin
Pete O'Hanlon19-Feb-13 13:06
mvePete O'Hanlon19-Feb-13 13:06 
GeneralApplication exit: Valid concern for SCENARIO 2 from Pete (Big thank you!) Pin
devvvy19-Feb-13 13:30
devvvy19-Feb-13 13:30 
GeneralRe: Thread.Sleep is NOT evil Pin
N a v a n e e t h19-Feb-13 6:36
N a v a n e e t h19-Feb-13 6:36 
GeneralRe: Thread.Sleep is NOT evil Pin
devvvy19-Feb-13 12:36
devvvy19-Feb-13 12:36 
AnswerRe: Thread.Sleep is NOT evil Pin
Dave Kreskowiak18-Feb-13 15:52
mveDave Kreskowiak18-Feb-13 15:52 

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

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