Best way for a thread to "wait on something"? [CS/C# 2005]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
So I have a thread (thRemote) that spawns several other threads (thTcpListener) to perform a certain task.
I am currently trying to create a mechanism that will allow my (thRemote) thread to "wait" for all (thTcpListener) threads to finish phase I (and subsequently somehow notify thRemote) before it continues... (thRemote should not move forward until the threads have completed the first couple of operations such as connecting, etc...)

So, in essense, I want to "pause" or "sleep" the thread (thRemote) until which time the last (thTcpListener) thread has reported back (somehow, still working on this) that it has completed phase I...
I imagine I could always use a WHILE LOOP and just loop until which time my bool becomes false (which would occur when all thTcpListeners reported back complete phase I) but that just sounds like bad programming - so I thought of maybe using Sleep()? or Suspend()? But sadly I have absolutly no experience with either and therefore I wanted to seek some guidance...

Any ideas, hints, and help would be greatly appreciated, thanks
 
A ManualResetEvent for each class w/ a call to WaitHandle.WaitAll should do the trick.

Each thTcpListener thread will have a ManualResetEvent and will call the ManualResetEvent.Set event when it reaches a point where the thRemote thread should continue.

The thRemote thread will call WaitHandle.WaitAll which will make the thRemote thread block/wait until all thTcpListener threads have invoked ManualResetEvent.Set.


http://msdn2.microsoft.com/en-us/library/z6w25xa6.aspx
http://msdn2.microsoft.com/en-us/library/system.threading.manualresetevent.aspx
 
Back
Top