views:

59

answers:

2

Hi folks, if a threadA is sleeping, how will another thread threadB call threadA to start ? Please provide an example if possible.

+1  A: 

A thread can be started by waiting on a WaitObject and having the other thread calling the Set method on it. Look at the WaitHandle.WaitOne method.

Here's article that may be of help as well.

Otávio Décio
+3  A: 

Instead of sleeping you will want to create an EventWaitHandle and use WaitOne with a timeout.

When you want the thread to wake-up early, you will simply set the event to signaled.

First create the EventWaitHandle:

wakeUpEvent = new EventWaitHandle(false, EventResetMode.ManualReset);

Then in your thread:

wakeUpEvent.WaitOne(new TimeSpan(1, 0, 0));

When the main program wants to wake up the thread early:

wakeUpEvent.Set();

Note: You can either set the event to auto reset or manual reset. Auto reset means once WaitOne returns from the event, it will set it back to non signaled. This is useful if you are in a loop and you signal multiple times.

Brian R. Bondy