Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted

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

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...