Monitor.Wait, Pulse - When worker thread should conditionally behave as an actual worker thread

Posted by Griever on Stack Overflow See other posts from Stack Overflow or by Griever
Published on 2010-05-12T11:37:34Z Indexed on 2010/05/12 12:04 UTC
Read the original article Hit count: 340

Filed under:
|
|
|

My particular scenario: - Main thread starts a worker thread. - Main thread needs to block itself until either worker thread is completed (yeah funny) or worker thread itself informs main thread to go on

Alright, so what I did in main thread:

wokerThread.Start(lockObj);
lock(lockObj)
 Monitor.Wait(lockObj);

Somewhere in worker thread:

if(mainThreadShouldGoOn)
 lock(lockObj)
  Monitor.Pulse(lockObj);

Also, at the end of worker thread:

lock(lockObj)
 Monitor.Pulse(lockObj);

So far, it's working perfect. But is it a good solution? Is there a better one?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about threads