HighTechTalks DotNet Forums  

How to wait for multiple STA threads to finish?

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss How to wait for multiple STA threads to finish? in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Sandeep
 
Posts: n/a

Default 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


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.