How to wait for multiple STA threads to finish? -
08-30-2006
, 04:06 PM
I create multiple STA threads and start them. Once I have started all
the threads in my main thread I need to wait till all of them are done.
To wait I tried
WaitHandle.WaitAll() ----- returns exception
Tried
waitEvent.waitOne()
and thread.Join()
In above case some times my main thread slips through Join\waitOne().
(i.e. main thread does not wait till all other threads are done)
Please advice how to implement this?
My sample code is as below
Main()
{
....
....
ArrayList myThreadClass = new ArrayList(3);
ArrayList threads = new ArrayList(3);
for(int i = 0 ; i< 3;i++)
{
ThreadClass thClass = new ThreadClass();
myThreadClass.Add(thClass);
Thread myThread = new Thread( new ThreadStart(thClass.threadMethod));
myThread.ApartmentState = ApartmentState.STA;
threads.Add(myThread);
((Thread)threads[i]).Start();
}
for(int k = 0;k<3;k++)
{
((Thread)threads[k]).Join();
}
......
......
} //Main Ends
Some more info
In my thread method I do some COM calls and make some callls Writeline
to StreamWriter |