Sammy Ageil

Disambiguation of Technologies

How to detect multiple threads completion in .Net

When working with multiple threads, sometimes we need to know when all threads have completed. while there are multiple ways to achieve this. Here is one way I found to be easy.
The key class used to achieve the task is WaitHandle  . lets start with a console app in C#

public static Random random = new Random();
      static void Main(string[] args)
      {
          //Initialize the WaitHadlers
          List<WaitHandle> waitHandles = new List<WaitHandle>();
 
          var manualResetEvent = new ManualResetEvent(false);
          waitHandles.Add(manualResetEvent);
          var taskOne = Task.Factory.StartNew(() => new Thread(JobOne).Start(manualResetEvent));
          taskOne.Wait();
 
          manualResetEvent = new ManualResetEvent(false);
          waitHandles.Add(manualResetEvent);
          var taskTwo = Task.Factory.StartNew(() => JobTwo(manualResetEvent, "A"));
          taskTwo.Wait();
 
          manualResetEvent = new ManualResetEvent(false);
          waitHandles.Add(manualResetEvent);
          var taskThree = Task.Factory.StartNew(() => new Thread(JobThree).Start(manualResetEvent));
          taskThree.Wait();
 
          var waitTask = Task.Factory.StartNew(() => WaitHandle.WaitAll(waitHandles.ToArray()));
          waitTask.Wait();
          Console.WriteLine("-----------------All jobs completed at {0}----------------", DateTime.Now);
          Console.ReadLine();
      } 

Here is the methods JobOne, Jobtwo and JobThree

private static void JobOne(Object obj)
{
    ManualResetEvent manualResetEvent = (ManualResetEvent)obj;
 
    // simulate a long runningt task
    Console.WriteLine("Job One started at {0} ", DateTime.Now);
    System.Threading.Thread.Sleep(random.Next(5000, 20000)); // random 5 to 20 second delay
    Console.WriteLine("Job One Completed at {0} ", DateTime.Now);
 
    manualResetEvent.Set();
}
 
private static void JobTwo(Object obj, string a)
{
    ManualResetEvent manualResetEvent = (ManualResetEvent)obj;
 
    // simulate some work:
    Console.WriteLine("Job Two started at {0} ", DateTime.Now);
    System.Threading.Thread.Sleep(random.Next(5000, 20000));
    Console.WriteLine("Job Two Completed at {0} ", DateTime.Now);
 
    manualResetEvent.Set();
}
 
private static void JobThree(Object obj)
{
    ManualResetEvent manualResetEvent = (ManualResetEvent)obj;
 
    // simulate some work:
    Console.WriteLine("Job Three started at {0} ", DateTime.Now);
    System.Threading.Thread.Sleep(random.Next(5000, 20000));
    Console.WriteLine("Job Three Completed at {0} ", DateTime.Now);
 
    manualResetEvent.Set();
}

You can achieve the same results using the Tasks namespace
See http://www.sammyageil.com/post/2011/06/14/Execute-multiple-Tasks-in-Net-40.aspx

Comments (1) -

  • diamond

    2/23/2014 4:05:24 PM |

    The post has actually been an eye opener, happy to have discovered it.

Pingbacks and trackbacks (1)+

Comments are closed