Click here to Skip to main content
15,886,519 members
Home / Discussions / C#
   

C#

 
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 
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 
Pete O'Hanlon wrote:
then why not use a timer?

because there's no reason to - as 99% of app don't need to know exactly when next while loop get executed, so why complicate things which are otherwise simpler? it's as *dangerous* as not knowing exactly when next loop get executed which 99% of applications don't care/matter

Pete O'Hanlon wrote:
it's so discussed is that it does have drawbacks, and it can be inherently risky. I

Using Thread.Sleep for SCENARIO 1/3 is just plain wrong. This complicates the otherwise simple discussion. Things which are simple should remain simple.

Pete O'Hanlon wrote:
don't use something because it saves you a few keystrokes, use the tool that is right and appropriate.

But I was just contemplating using your suggestion wrapping timer into a single line function. As I do agree - after you wrap it to single line,
a. it's as simple as thread.sleep
b. now you know exactly when next execution happens


Pete O'Hanlon wrote:
Suppose that your application creates 10 threads and then puts them to sleep for 30 seconds, and each thread has opened up some resources that must be disposed of when the thread completes. Now, suppose that 2 seconds into this, your application wants to close - what do you think the effect will be?


This is a Thread.Abort or App Exit clean up issue, not Thread.Sleep issue - it's always bad to forcefully terminate app/threads. I take what you mean as following?

<br />
try {<br />
   while(true) {<br />
       using(Stream...)<br />
       {<br />
            ... <br />
            Thread.Sleep(#1) <-- What would happen? indeterministic<br />
       } <-- Dispose here anyway as "using" translated to try-finally (But what if aborted during finally)<br />
<br />
       Thread.Sleep(#2) <-- fine already disposed<br />
    }<br />
catch(ThreadAbortEx)<br />
{<br />
<br />
}<br />
finally{<br />
<br />
}<br />

dev

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 
GeneralRe: Thread.Sleep is NOT evil Pin
devvvy19-Feb-13 1:38
devvvy19-Feb-13 1:38 
GeneralRe: Thread.Sleep is NOT evil Pin
Dave Kreskowiak19-Feb-13 1:46
mveDave Kreskowiak19-Feb-13 1:46 
GeneralRe: Thread.Sleep is NOT evil Pin
devvvy19-Feb-13 1:49
devvvy19-Feb-13 1:49 
GeneralRe: Thread.Sleep is NOT evil Pin
Dave Kreskowiak19-Feb-13 2:19
mveDave Kreskowiak19-Feb-13 2:19 
GeneralRe: Thread.Sleep is NOT evil Pin
devvvy19-Feb-13 12:38
devvvy19-Feb-13 12:38 

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.