Click here to Skip to main content
15,887,596 members
Home / Discussions / C#
   

C#

 
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 
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 
Thread.Sleep() is not EVIL if you know how it works and if that's the behavior that you really want to accomplish. The reason people say it is evil because they use it wrongly without understanding how it works.
devvvy wrote:

SCENARIO 2 - humble timing while loop: I'm not hearing any reason not to use it. As indicated
If your application doesn't care about accuracy of the execution ineterval, then Thread.Sleep() is alright to use. Let us say you need to execute some task every minute and you start the application at 10AM. It is supposed to execute at 10.01, 10.02, 10.03 etc. You'd write something like,
C#
while(!exit)
{
    // Do your job
    Thread.Sleep(10000);
}
What happens if your job takes more than 1 minute? Then the next run which was supposed to happen at 10.01 won't happen. This delays the next execution too. With this design, it will be hard to tell when the next run will happen. If this behavior is alright for your application, there is no problem in using Thread.Sleep(). To workaround this problem, you can use System.Threading.Timer to schedule the job. It gives a better scheduling capabilities and executes the job exactly at the interval that you specify. Timer callback method can be executed simultaneously by multiple threads if the job takes more time than the interval. This increases parallelism and makes the processing faster.

It is also logical to think that why do you need to waste a thread by just sleeping? The wastage could be minimal, but it is a wastage. Where System.Threading.Timer uses thread pool threads and thread pools are more optimized for better usage of resources. Thread pools are initialized with a set of threads when the application domain starts.
devvvy wrote:

This is an over discussed subject, gives people wrong impression this is actually complicated, that Thread.Sleep is really evil.
It's about maturity. If you know how Thread.Sleep() works then you'd probably use it correctly and use alternatives where Thread.Sleep() is not the right solution. Any statement which says Thread.Sleep() is evil without understanding the context where it is used is STUPID.
Best wishes,
Navaneeth


modified 18-Feb-13 23:36pm.

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 
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 

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.