Click here to Skip to main content
15,898,222 members
Home / Discussions / C#
   

C#

 
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 
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 
Pete O'Hanlon wrote:
There is if you're trying to shut down an application. I don't know how many times I can point out the same thing; perhaps with a simple example - you have a service with a thread that needs to clean up before the service can shut down - by having to wait for the thread to finish, you could trigger the "Service could not be stopped, blah blah blah" message.

My apologies Pete - this is something we'd run into if we're not careful with SCENARIO 2 if Thread.Sleep(LONGTIME) (where timer/handler would have avoided this). Having consider this, Thread.Sleep even for SCENARIO 2 may likely to cause problem in app exit if not used carefully. I don't think this very important argument has nearly enough visibility in this discussion/other places. Thanks Pete!

Thread.Sleep is Evil after all. (For SCENARIO 1,2,3)

Sample to demo the issue as follows:
<br />
static void Main(string[] args)<br />
        {<br />
            Thread t = new Thread(new ThreadStart(ThreadFunc));<br />
            t.Start();<br />
<br />
            Console.WriteLine("Hit any key to exit.");<br />
            Console.ReadLine();<br />
<br />
            Console.WriteLine("App exiting");<br />
            return;<br />
        }<br />
<br />
        static void ThreadFunc()<br />
        {<br />
            int i=0;<br />
            try<br />
            {<br />
                while (true)<br />
                {<br />
                    Console.WriteLine(i);<br />
                    Thread.Sleep(1000 * 10);<br />
                    i++;<br />
                }<br />
            }<br />
            finally<br />
            {<br />
                Console.WriteLine("Exiting while loop");<br />
            }<br />
            return;<br />

dev

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 
GeneralRe: Thread.Sleep is NOT evil Pin
Dave Kreskowiak19-Feb-13 14:03
mveDave Kreskowiak19-Feb-13 14:03 

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.