Open In App

Joining Threads in C#

Improve
Improve
Like Article
Like
Save
Share
Report

In C#, you can create and run multiple threads simultaneously to perform different tasks concurrently. Sometimes, it is necessary to wait for one thread to complete before starting another thread or wait for all threads to complete before proceeding further. To achieve this, you can use the following methods to join threads in C#:

Thread.Join(): The Join method of the Thread class waits for the thread to complete its execution before continuing with the execution of the calling thread. This method blocks the calling thread until the target thread has completed its execution.
Example:

C#




using System;
using System.Threading;
 
class Program
{
    static void Main(string[] args)
    {
        Thread t1 = new Thread(() => Console.WriteLine("Thread 1"));
        Thread t2 = new Thread(() => Console.WriteLine("Thread 2"));
 
        t1.Start();
        t2.Start();
 
        t1.Join();
        t2.Join();
 
        Console.WriteLine("Main Thread");
    }
}


Output

Thread 1
Thread 2
Main Thread

2.Task.WaitAll(): The WaitAll method of the Task class waits for all the specified tasks to complete before continuing with the execution of the calling thread. This method blocks the calling thread until all the specified tasks have completed their execution.
Example:

C#




using System;
using System.Threading.Tasks;
 
class Program
{
    static void Main(string[] args)
    {
        Task t1 = Task.Run(() => Console.WriteLine("Task 1"));
        Task t2 = Task.Run(() => Console.WriteLine("Task 2"));
 
        Task.WaitAll(t1, t2);
 
        Console.WriteLine("Main Thread");
    }
}


Output

Task 1
Task 2
Main Thread

In C#, Thread class provides the Join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t.Join() causes the current thread to pause its execution until thread it joins completes its execution. If there are multiple threads calling the Join() methods that mean overloading on join allows the programmer to specify a waiting period. However, as with sleep, Join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify. There are three methods in the overload list of Thread.Join() method as follows:

  • Join() Method: This method blocks the calling thread until the thread represented by this instance terminates while continuing to perform standard COM and SendMessage pumping. Syntax:
public void Join ();
  • Join(Int32) Method: This method blocks the calling thread until the thread represented by this instance terminates or the specified time elapses while continuing to perform standard COM and SendMessage pumping. Syntax:
public bool Join (int millisecondsTimeout);
  • Join(TimeSpan) Method: This method blocks the calling thread until the thread represented by this instance terminates or the specified time elapses while continuing to perform standard COM and SendMessage pumping. Syntax:
public bool Join (TimeSpan timeout);

Example 1: 

CSharp




// C# program to illustrate the
// concept of Join() method
using System;
using System.Threading;
 
public class ExThread
{
 
    // Non-Static method
    public void mythread()
    {
        for (int x = 0; x < 4; x++)
        {
            Console.WriteLine(x);
            Thread.Sleep(100);
        }
    }
 
    // Non-Static method
    public void mythread1()
    {
        Console.WriteLine("2nd thread is Working..");
    }
}
 
// Driver Class
public class ThreadExample
{
    // Main method
    public static void Main()
    {
        // Creating instance for
        // mythread() method
        ExThread obj = new ExThread();
         
        // Creating and initializing threads
        Thread thr1 = new Thread(new ThreadStart(obj.mythread));
        Thread thr2 = new Thread(new ThreadStart(obj.mythread1));
        thr1.Start();
         
        // Join thread
        thr1.Join();
        thr2.Start();
         
    }
}


Output:

0
1
2
3
2nd thread is Working..

Explanation: In the above example, we have a class named as ExThread and this class contains a non-static method, i.e. mythread() and mythread1(). So we create an instance, i.e. obj of ExThread class and refer it in the constructor of ThreadStart class. Using Thread thr1 = new Thread(new ThreadStart(obj.mythread)); statement we create a thread named as thr1 and initialize the work of this thread, similarly for thread thr2. By using thr1.Join(); statement, we will send the calling of thr2 into waiting until the work of thr1 thread is completed. After that thr2 thread executes. Example 2: 

CSharp




// C# program to illustrate the
// concept of Join() method
using System;
using System.Threading;
  
class GFG {
  
    // Creating TimeSpan for thread
    static TimeSpan mytime = new TimeSpan(0, 0, 1);
  
    // Main method
    public static void Main()
    {
        // Creating and initializing new thread
        Thread thr = new Thread(mywork);
        thr.Start();
  
        if (thr.Join(mytime + mytime)) {           
            Console.WriteLine("New thread is terminated");
        }
        else {
            Console.WriteLine("Join timed out");
        }
    }
  
    static void mywork()
    {
        Thread.Sleep(mytime);
    }
}


Output:

New thread is terminated

Reference: 



Last Updated : 21 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads