C# – Joining Threads

By | 22/09/2021

In this post, we will see how to use the Join method with the Threads in order to allows one thread to wait until another completes the execution.

We start creating a Console application where we add three methods in Program.cs called Method1, Method2 and Method3:

using System;
using System.Threading;

namespace MultiThreading
{
    class Program
    {
        static void Main(string[] args)
        {
            
        }

        private static void Method1()
        {
            for(int i=1; i<5; i++)
            {
                Console.WriteLine($"Method1 {i}");
                Thread.Sleep(2000);
            }
        }

        private static void Method2()
        {
            for (int i = 6; i < 11; i++)
            {
                Console.WriteLine($"Method2 {i}");
                Thread.Sleep(2000);
            }
        }

        private static void Method3()
        {
            for (int i = 12; i < 17; i++)
            {
                Console.WriteLine($"Method3 {i}");
                Thread.Sleep(2000);
            }
        }
    }
}



Then, we modify Main method in order to run this methods in three different threads:

using System;
using System.Threading;

namespace MultiThreading
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread firstThread = new Thread(Method1);
            Thread secondThread = new Thread(Method2);
            Thread thirdThread = new Thread(Method3);

            firstThread.Start();
            secondThread.Start();
            thirdThread.Start();
        }

        ...
        ...
        ...
    }
}



If we run the application, this will be the result:

With the Join method, we can choose to run Method1 and Method2 in the same time and instead we will run Method3 only when Method2 is finished.
In order to do it, we change the code in the Main method in this way:

using System;
using System.Threading;

namespace MultiThreading
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread firstThread = new Thread(Method1);
            Thread secondThread = new Thread(Method2);
            Thread thirdThread = new Thread(Method3);

            firstThread.Start();
            secondThread.Start();
            secondThread.Join();
            thirdThread.Start();
        }

        ...
        ...
        ...
    }
}



If we run the application, this will be the result:

In the same way, we can run first Method1 and when it is finished, we will run Method2 and Method3:

using System;
using System.Threading;

namespace MultiThreading
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread firstThread = new Thread(Method1);
            Thread secondThread = new Thread(Method2);
            Thread thirdThread = new Thread(Method3);

            firstThread.Start();
            firstThread.Join();
            secondThread.Start();
            thirdThread.Start();
        }

        ...
        ...
        ...
    }
}



If we run the application, this will be the result:

Finally, we can specify how long the thread will wait for as well:

using System;
using System.Threading;

namespace MultiThreading
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread firstThread = new Thread(Method1);
            Thread secondThread = new Thread(Method2);
            Thread thirdThread = new Thread(Method3);

            Console.WriteLine(DateTime.Now);
            firstThread.Start();
            // the value is in millisecond
            // in this case, the second and third thread will start after 4 seconds
            firstThread.Join(4000);
            Console.WriteLine(DateTime.Now);
            secondThread.Start();
            thirdThread.Start();
        }

        ...
        ...
        ...
    }
}



If we run the application, this will be the result:



Category: C# Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *