Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining when all threads have finished c#

I am very new to threading, as a beginner in C#. I have a program that will be firing multiple threads inside of a windows app. My aim is to start a new thread for every item within a list. The items in this list are workstation names on anetwork. Each thread that is created will look to do repairs on each machine, when the thread has finished it will write to a log file of any errors found etc. But what i want to be able to determine is when all threads have finished. So if i have 100 machines, 100 threads, how do i determine when all have closed?

Heres my method below :-

private void repairClientsToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (machineList.Count() != 0)
    {
        foreach (string ws in machineList)
        {
            new Thread(new ParameterizedThreadStart(fixClient), stack).Start(ws);
        }
    }
    else
    {
         MessageBox.Show("Please import data before attempting this procedure");
    }
}
like image 641
Derek Avatar asked Apr 24 '12 08:04

Derek


People also ask

How do you know when a thread is completed?

The isAlive() method is used to check if a thread has finished executing. If the thread is still running, it returns true, otherwise false.

Which method is used to check if the thread execution is finished?

A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as IsAlive to check if the thread is alive or not. Or in other words, the value of this property indicates the current execution of the thread.

Do threads end automatically?

A thread automatically terminates when it returns from its entry-point routine. A thread can also explicitly terminate itself or terminate any other thread in the process, using a mechanism called cancelation.

How do you wait for threads to finish?

When using an Executor, we can shut it down by calling the shutdown() or shutdownNow() methods. Although, it won't wait until all threads stop executing. Waiting for existing threads to complete their execution can be achieved by using the awaitTermination() method.


2 Answers

The way to do this would be to keep a reference to all the threads and then Join on them. This basically means that the current thread will block until the joined thread completes.

Change your loop to something like:

foreach (string ws in machineList)
{
   var thread = new Thread(new ParameterizedThreadStart(fixClient), stack);
   _machineThreads.Add(thread)
   thread.Start();
}

(where _machineThreads is a list of System.Thread)

You can then block until all are complete with something like:

private void WaitUntilAllThreadsComplete()
{
   foreach (Thread machineThread in _machineThreads)
   {
      machineThread.Join();
   } 
}

HOWEVER - you almost certainly don't want to be doing this for the scenario you describe:

  • You shouldn't create a large number of threads - explicitly creating hundreds of threads is not a great idea
  • You should prefer other approaches - try looking at Parallel.ForEach and System.Threading.Task. These days, .Net gives you lots of help when working with threading and asynchronous tasks - I really would advise you read up on it rather than trying to "roll your own" with explicit threads.
  • This looks like a click handler. Is it ASP.NET web forms, or a desktop application? If the former, I certainly wouldn't advise spawning lots of threads to perform background tasks from the request. In either case, do you really want your web page or GUI to block while waiting for the threads to complete?
like image 185
Rob Levine Avatar answered Sep 19 '22 06:09

Rob Levine


you can use: IsAlive. But you have keep a reference like

 Thread t = new Thread(new ThreadStart(...));
 t.start();
 if(t.IsAlive)
 {
    //do something
 }
 else
 {
    //do something else
 }
like image 23
jorne Avatar answered Sep 19 '22 06:09

jorne