Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Detect when threads are complete

Posted on 2011-03-16
6
Medium Priority
?
632 Views
Last Modified: 2012-06-27
I have a console application that creates multiple threads that all run at the same time. I need to detect when all the threads are complete so I can start a new method. How do I detect when all threads are finished running???

 
public static Thread workerThread1;
public static Thread workerThread2;
public static Thread workerThread3;
        
   static void Main(string[] args)
   {
       workerThread1 = new Thread(RiteAid);
       workerThread1.Start();
 
       workerThread2 = new Thread(Kmart);
       workerThread2.Start();

       workerThread3 = new Thread(Walgreens);
       workerThread3.Start();
}

Open in new window

0
Comment
Question by:jtwoods4
6 Comments
 
LVL 10

Expert Comment

by:ALaRiva
ID: 35150761
I would say the easiest solution would be to use a While(true) loop that checks the .IsAlive property of each thread, and if ALL return false, then break out of the loop, and you will know that all threads are complete.

hth

- Anthony
0
 

Author Comment

by:jtwoods4
ID: 35150774
That will cause a stack overflow because the threads run for several hours.
0
 
LVL 10

Expert Comment

by:ALaRiva
ID: 35150907
Gotcha, that wasn't mentioned initially.

I've not worked with threads that run over such an extended time, so I will have to defer to others. I found a few links here that might help you.

http://stackoverflow.com/questions/2399536/how-do-you-determine-if-child-threads-have-completed

http://stackoverflow.com/questions/263116/c-waiting-for-all-threads-to-complete

Both talk a bit about using the thread joining method, which I should have mentioned, but it goes into a bit more detail and might be a reasonable solution.

hth

- Anthony
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 35151292
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 35151535
Here's one approach:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {

        public static Random R = new Random();

        static void Main(string[] args)
        {
            List<WaitHandle> handles = new List<WaitHandle>();

            ManualResetEvent mre = new ManualResetEvent(false);
            handles.Add(mre);
            new Thread(new ParameterizedThreadStart(RiteAid)).Start(mre);

            mre = new ManualResetEvent(false);
            handles.Add(mre);
            new Thread(new ParameterizedThreadStart(Kmart)).Start(mre);

            mre = new ManualResetEvent(false);
            handles.Add(mre);
            new Thread(new ParameterizedThreadStart(Walgreens)).Start(mre);

            WaitHandle.WaitAll(handles.ToArray());
        }

        private static void RiteAid(Object obj)
        {
            ManualResetEvent mre = (ManualResetEvent)obj;

            // simulate some work:
            Console.WriteLine("RiteAid started...");
            System.Threading.Thread.Sleep(R.Next(3000, 10000)); // random 3 to 10 second delay
            Console.WriteLine("RiteAid done.");

            mre.Set();
        }

        private static void Kmart(Object obj)
        {
            ManualResetEvent mre = (ManualResetEvent)obj;

            // simulate some work:
            Console.WriteLine("Kmart started...");
            System.Threading.Thread.Sleep(R.Next(3000, 10000)); // random 3 to 10 second delay
            Console.WriteLine("Kmart done.");

            mre.Set();
        }

        private static void Walgreens(Object obj)
        {
            ManualResetEvent mre = (ManualResetEvent)obj;

            // simulate some work:
            Console.WriteLine("Walgreens started...");
            System.Threading.Thread.Sleep(R.Next(3000, 10000)); // random 3 to 10 second delay
            Console.WriteLine("Walgreens done.");

            mre.Set();
        }

    }
}

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35151780
As ALaRiva mentioned you should try Thread.Join(). It should be something like this:

public static Thread workerThread1;
public static Thread workerThread2;
public static Thread workerThread3;
        
   static void Main(string[] args)
   {
       workerThread1 = new Thread(RiteAid);
       workerThread1.Start();
 
       workerThread2 = new Thread(Kmart);
       workerThread2.Start();

       workerThread3 = new Thread(Walgreens);
       workerThread3.Start();

       workerThread1.Join();
       workerThread2.Join();
       workerThread3.Join();
}

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question