Thread.Sleep () sleeps longer
I have a Winform that has to wait about 3 - 4 hours. I cannot close and somehow reopen the application as it does several things in the background while it waits.
To achieve the wait - without causing problems with the UI thread and for other reasons, I have a BackgroundWorker to which I send how many milliseconds to wait and call Thread.Sleep(waitTime);
on my doWork event. In an event, backGroundWorker_RunWorkerCompleted
I do what the program should do after waiting.
This works great on a development machine. that is, the wait ends when it ends. But on the test machine, it continues to wait longer. This happened two times, the first time he waited exactly 1 hour more than the indicated time, and the second time he waited more about 2 hours and 40 minutes.
Could there be some obvious reason for this or am I missing something?
Dev machine is Win XP and test machine is Win 7.
source to share
Check out this article that explains the reason:
Thread.Sleep (n) means block the current thread for at least the number of time series (or thread quanta) that can occur within n milliseconds. The length of the timeline differs from different versions / types of Windows and different processors and generally ranges from 15 to 30 milliseconds. This means that the thread is almost guaranteed to block in more than milliseconds. The likelihood that your thread will re-wake up after exactly n milliseconds is about as impossible as impossible as possible. So Thread.Sleep is meaningless for the time being.
BTW, this also explains why not use Thread.Sleep;)
I agree with other recommendations to use Timer instead of Thread.Sleep.
source to share
I suggest using ManualResetEvent instead:
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx
ManualResetEvent mre = new ManualResetEvent(false);
mre.WaitOne(waitTime);
...
//your background worker process
mre.Set();
As a bonus, you will be able to interrupt this dream faster.
source to share
In my humble opinion, the difference in latency cannot be explained by the information you have given us alone. I would really think that the reason for the difference lies in the moment the sleep starts. So the actual challenge Thread.sleep(waitTime);
. Are you sure the dream was triggered the moment you think it is?
And, as suggested in the comment, if you really have to wait a long time for this; consider using Timer
to start required events. Or even scheduling of some kind, within your application. Of course, this depends on your actual implementation and therefore may be easier said than done. But he "feels" stupid, allowing background work to sleep for so long.
source to share
PREFIX: This requires .NET 4 or newer
Think about how to make your function asynchronous and just do:
await Task.Delay(waitTime);
Alternatively, if you can't make your function asynchronous (or don't want to), you can also:
Task.Delay(waitTime).Wait();
This is a one line solution and anyone with a copy of Reflector can check that Task.Delay is using a timer internally.
source to share