user34537
user34537

Reputation:

Passing a msg to another thread in C#

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'

Upvotes: 1

Views: 255

Answers (4)

Nader Shirazie
Nader Shirazie

Reputation: 10776

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/

Upvotes: 3

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

use AutoresetEvent or ManualResetEvent

Upvotes: 2

Svetlozar Angelov
Svetlozar Angelov

Reputation: 21660

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

Upvotes: 1

Scott Whitlock
Scott Whitlock

Reputation: 13839

Use the System.Threading.Semaphore class.

Upvotes: 0

Related Questions