Solved

Detect when threads are complete

Posted on 2011-03-16
6
629 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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