Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Detect when threads are complete

Posted on 2011-03-16
6
626 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 35151292
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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