I have a Winform which needs to wait for about 3 – 4 hours. I can’t close and somehow reopen the App, as it does few things in background, while it waits.
To achieve the wait – without causing trouble to 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); in its doWork event. In the backGroundWorker_RunWorkerCompleted event, I do what the program is supposed to do after the wait.
This works fine on the development machine. i.e. the wait ends when it has to end. But on the Test machine, it keeps waiting for longer. It happened two times, first time it waited exactly 1 hour more than specified time and second time it waited more for about 2 Hours and 40 minutes.
Could there be any obvious reason for this to happen or am I missing something?
The dev machine is Win XP and Test machine is Win 7.
Have a look at this article which explains the reason:
By the way it also explains why not to use Thread.Sleep 😉
I agree to the other recommendations to use a Timer instead of the Thread.Sleep.