Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, May 13, 2006 12:25 PM
Hello friends,
Kindly fo through the following code sample,
In this code, i want to stop execution till all the threads have complited their job. and i dont know how can i do this.
static ArrayList FarchedContent = new ArrayList();
public static string SearchContent(string KeyWord)
{
objThread = new Thread[20];
for (int i = 0; i < 20; i++)
{
objThread = new Thread(new ThreadStart(FatchContent));
objThread.Priority = ThreadPriority.AboveNormal;
objThread.Start();
}
//Here I want to use FarchedContent but i want to make sure that all the //above created threads have complited their job.
// How can I do this ??
}
private static void FatchContent()
{
string str;
//some task;
// Assign content to str
FarchedContent.Add(str);
Thread.CurrentThread.Abort();
}
can any one know how can i do this ?
Rgds,
Kiran Suthar
All replies (4)
Saturday, May 13, 2006 9:25 PM ✅Answered
I believe Thread.Join will do what you want if you create a Thread specifically. Or you can use AutoResetEvent.WaitOne if you use a thread from the thread pool.
AutoResetEvent autoEvent = new AutoResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallBack(SomeMethod), autoEvent);autoEvent.WaitOne();
or
Thread thread = new Thread(new ThreadStart(SomeOtherMethod));
thread.Start();thread.Join();
Monday, May 15, 2006 12:46 PM ✅Answered
You can join all thread ones:
public static string SearchContent(string KeyWord) { Thread[] objThread = new Thread[20]; for (int i = 0; i < 20; i++) { objThread[ i ] = new Thread(new ThreadStart(FatchContent)); objThread[ i ].Priority = ThreadPriority.AboveNormal; objThread[ i ].Start(); } for( int i = 0; i < objThread.Length; i ++ ) { // Wait until thread is finished. objThread[ i ].Join(); } // TODO: Fetch result and return. } |
Or you can use the WaitHandle.WaitAll method, see the example it the documentation on that page.
Saturday, May 13, 2006 6:37 PM
hi,
you can get the process threads ,i guess in .net each process has at least three threads if its more than three then its your own created threads
i guess after you finish creating threads you can write a loop method to check the number of threads as long as the thread count more than 3 to enter another loop till all threads finish its jobs
Process thisProc = Process.GetCurrentProcess();
ProcessThreadCollection mythreads = thisProc.Threads;
this is my personal opinion but i don't this is a good thing to use threads then because we suppose to use threads when we don't want to interrupt the program folow if you want to interrupt the program folow then use normal loops
hope this helps
Friday, January 12, 2007 9:07 AM
The key is indeed to use the WaitHandle.WaitAll method.
The method has a WaitHandle[] as it's parameter, that can contain Events. So, basically you'll have to pass an Event to your thread method, for it to signal it when it exits. In your main procedure, you launch your threads and then call WaitHandle.WaitAll(handlesArray). I guess you can also pass a timeout time to this method if you feel so. Read the WaitHandle documentation, it contains usefull examples, and other usefull functions.
Here is what your code would look like:
static ArrayList FarchedContent = new ArrayList();
public static string SearchContent(string KeyWord)
{
/* Instead of this, use a ThreadPool that also allows you to pass an object to the main function of the thread
objThread = new Thread[20];
for (int i = 0; i < 20; i++)
{
objThread[ i ] = new Thread(new ThreadStart(FatchContent));
objThread[ i ].Priority = ThreadPriority.AboveNormal;
objThread[ i ].Start();
}
*/
AutoResetEvent[] eventArray = new AutoResetEvent[20];
for(int i = 0; i < 20; i++)
{
//create this thread's event:
eventArray[ i ] = new AutoResetEvent(false);
//ask ThreadPool to execute your thread
ThreadPool.QueueUserWorkItem(new WaitCallback(FatchContent), eventArray[ i ]);
}
//Here I want to use FarchedContent but i want to make sure that all the //above created threads have complited their job.
// How can I do this ??
//wait for all the events to be signaled:
WaitHandle.WaitAll(eventArray);
//here you can use FarchedContent, you can be sure that all threads have done their job
}
private static void FatchContent(AutoResetEvent resetEvent)
{
string str;
//some task;
// Assign content to str
//should consider synchronizing your access to the FarchedContent array
FarchedContent.Add(str);
//you don't need to abort your thread -> when it's main function is done, it exists
//Thread.CurrentThread.Abort();
//set the event to signal that this thread has ended it's job:
resetEvent.Set();
}
And that's it!
If you really need to use Thread objects in your SearchContent function, create your own class that will contain a reference to the FarchedContent array, a reference to a resetEvent which you can pass at construction time, and a FatchContent function with no parameters, so you can pass it to the ThreadStart constructor.
It will be something like:
MyThreadClass myTh = new MyThreadClass(farchedContentArray, thisThreadsEvent);
ThreadStart ts = new ThreadStart(myTh.FatchContent);