Resuming a sleeping thread
-
Hi. I have created a thread that calls Thread.Sleep(60000) after doing some work, and then it does some more work. What I wanted to do is to make the thread to resume processing and not having to wait the 60000ms until the sleep ends. For example, a function that tests if the thread is asleep, and if so, wakes it up. Is there a way to do this? I tried with the 'Resume' function, but this works only if the thread is in 'suspended' state. Thanks.
-
Hi. I have created a thread that calls Thread.Sleep(60000) after doing some work, and then it does some more work. What I wanted to do is to make the thread to resume processing and not having to wait the 60000ms until the sleep ends. For example, a function that tests if the thread is asleep, and if so, wakes it up. Is there a way to do this? I tried with the 'Resume' function, but this works only if the thread is in 'suspended' state. Thanks.
-
Try to "Suspend" thread insted of "Sleep". My english is not so good. Please, correct my errors. Best regards, Alexey.
Thanks for the advice, but Suspend just suspends the thread infinitely. The normal behaviour of the thread should be: DoSomeWork Thread.Sleep(60000) DoSomeMoreWork But in certain occasions I need to tell the thread to execute 'DoSomeMoreWork' before the sleep ends, that is, tell it to stop sleeping and continue execution.
-
Thanks for the advice, but Suspend just suspends the thread infinitely. The normal behaviour of the thread should be: DoSomeWork Thread.Sleep(60000) DoSomeMoreWork But in certain occasions I need to tell the thread to execute 'DoSomeMoreWork' before the sleep ends, that is, tell it to stop sleeping and continue execution.
-
Thanks for the advice, but Suspend just suspends the thread infinitely. The normal behaviour of the thread should be: DoSomeWork Thread.Sleep(60000) DoSomeMoreWork But in certain occasions I need to tell the thread to execute 'DoSomeMoreWork' before the sleep ends, that is, tell it to stop sleeping and continue execution.
-
Use timer to manage this situation. My english is not so good. Please, correct my errors. Best regards, Alexey.
Use .net synchronisation mechanism . If u want ur worker thread to wait for a specific event use wait handles for the same. It is recomended not to use sleep to get the necessary syncronisation.
-
Hi. I have created a thread that calls Thread.Sleep(60000) after doing some work, and then it does some more work. What I wanted to do is to make the thread to resume processing and not having to wait the 60000ms until the sleep ends. For example, a function that tests if the thread is asleep, and if so, wakes it up. Is there a way to do this? I tried with the 'Resume' function, but this works only if the thread is in 'suspended' state. Thanks.
Use one of the many synchronization objects that .NET provides. Take a look at AutoResetEvent[^], ManualResetEvent[^] and Mutex[^]. In all the cases, you would have to do something like
Thread A
DoX();
syncObject.WaitOne(); // Wait until Thread B signals
DoY();Thread B
DoP();
syncObject.Release(); // Signal Thread A to resume operations.
DoQ();The advantage of using synchronization objects instead of looping with a flag is that threads waiting on a synchronization primitive do not consume CPU cycles. Windows knows about synchronization primitives and so doesn't schedule the thread for execution until the sync object is signalled. With looping, Windows is forced to schedule that thread and it uses up its time slice looping.
Regards Senthil _____________________________