Solved

Detect when threads are complete

Posted on 2011-03-16
6
628 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
[X]
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
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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 35151292
0
 
LVL 86

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# DataTable (in memory) vs SQL Lookups 4 75
Telerik RadEditor Control Save 8 39
Pdf not loading 2 40
Concurrency Error Persistent using Data Grid View Update C# 2 31
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 long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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