Solved

Detect when threads are complete

Posted on 2011-03-16
6
624 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
Comment Utility
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
Comment Utility
That will cause a stack overflow because the threads run for several hours.
0
 
LVL 10

Expert Comment

by:ALaRiva
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 11

Expert Comment

by:Kusala Wijayasena
Comment Utility
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now