Unfortunately .NET doesn't have a built in semaphore primitive but you can
acheive the same effect with a counter and a ManualResetEvent. The algorithm
below can easily be encapsulated in a Semaphore class {I have one somewhere}:
You will need two variables at a scope that both the main thread and the
worker threads can access:
int numActiveThreads = 0;
ManualResetEvent waitEvent = new ManualResetEvent(false);
Have the MAIN THREAD issue Interlocked.Increment(&numActiveThreads) and then
spin off a worker thread - IN THAT ORDER - increment on main thread/spin off
worker until the desired number of threads have been started. After the
desired number of threads have been started let the main thread sleep on the
unsignalled event.
As each worker thread finishes have it issue
Interlocked.Decrement(&numActiveThreads) and test the result for <= 0. If
<= 0 then the last thread is finishing and you can wake the main thread...
--Richard
"Joe" wrote:
I need to start 8 threads and have the app or calling thread wait until all
threads are done. I was thinking of using Join but that will block the
thread before any other thread could be started.
I haven't done a lot with threads in C# yet so I could really use some help.
Thanks,
Joe