views:

152

answers:

4

I have a thread running in the background. How do i send it messages from my main thread? The only msg i need to send is 'go'/'wakeup'

A: 

Use the System.Threading.Semaphore class.

Scott Whitlock
+1  A: 

If you are talking about waking up a sleeping thread - Thread.Suspend()(wait) - Thread.Resume()(go/wake up)

Svetlozar Angelov
+2  A: 

use AutoresetEvent or ManualResetEvent

ArsenMkrt
+3  A: 

If your thread is doing nothing till you want it to start, then why start it until you want it to go?

If you are looking to run your background thread, then pause/wait for some event, and then continue, a simple method is to use the EventWaitHandle family of classes.

A simple example (taken from this question). Both threads should have access to the following:

private ManualResetEvent _workerWait = new ManualResetEvent(false);

Then, in your worker thread:

_workerWait.WaitOne();

Now, it will block until your main thread calls:

_workerWait.Set()

For a more complete discussion of your options and some examples, see: http://www.albahari.com/threading/

Nader Shirazie