I am using .NET (C#).
if I have 2 threads running T1 and T2 and T1 is like this:
while (true)
{
dosomething(); //this is a very fast operation
sleep(5 seconds);
}
at the same time T2 is doing something completely different however from time to time it needs to give T1 a kick such that it wakes up from the sleep even though the sleep time is not up. How do I do this?
Use a WaitHandle, like ManualResetEvent (or AutoResetEvent).
In your class, declare a ManualResetEvent:
private ManualResetEvent myEvent = new ManualResetEvent(false);
Thread1:
while(true) {
doSomething();
myEvent.WaitOne(5000);
myEvent.Reset();
}
Thread2:
myEvent.Set();
Thread1 will wait for 5 seconds or until the ManualResetEvent is Set, whichever comes first.
EDIT: added AutoResetEvent above
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With