In the example below, I had three threads that I needed to track. Each thread, when this has been done, will set a corresponding descriptor.
Declare it as follows:
private WaitHandle[] waithandles; // see comment on static below
Create it as follows:
waitcount = 3; waithandles = new WaitHandle[3] { new AutoResetEvent(false), new AutoResetEvent(false), new AutoResetEvent(false) };
In the thread, when it is finished, set it like this:
((AutoResetEvent)waithandles[i]).Set();
(Actually, this is too simplistic, but it will work if you make waithandle static. What I actually did is a thread that calls back at the end of my life to signal waithandle)
In the main thread check this out. When the wait reached zero, I knew that all threads completed
while (waitcount > 0) { WaitHandle.WaitAny(waithandles, 30000); waitcount--; }
source share