IsAlive and Join Method of Thread class in C# 

IsAlive and Join Method of Thread class in C# with Examples

In this article, I am going to discuss IsAlive and Join Method of Thread class in C# with examples. Please read our previous article before proceeding to this article where we discussed how to return data from a thread function using call back method in C# with an example.

Let us understand the use of Join Method of Thread class with an example. Please have a look at the following example. In the below example we have created three methods and then execute the methods using three separate threads. The point that you need to remember is the threads thread1, thread2, and thread3 are called as the child threads of Main thread. This is because these three threads are created by the main thread only.

Example:
using System.Threading;
using System;
namespace ThreadingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main Thread Started");

            //Main Thread creating three child threads
            Thread thread1 = new Thread(Method1);
            Thread thread2 = new Thread(Method2);
            Thread thread3 = new Thread(Method3);

            thread1.Start();
            thread2.Start();
            thread3.Start();

            Console.WriteLine("Main Thread Ended");
            Console.Read();
        }

        static void Method1()
        {
            Console.WriteLine("Method1 - Thread1 Started");
            Thread.Sleep(3000);
            Console.WriteLine("Method1 - Thread 1 Ended");
        }

        static void Method2()
        {
            Console.WriteLine("Method2 - Thread2 Started");
            Thread.Sleep(2000);
            Console.WriteLine("Method2 - Thread2 Ended");
        }

        static void Method3()
        {
            Console.WriteLine("Method3 - Thread3 Started");
            Thread.Sleep(5000);
            Console.WriteLine("Method3 - Thread3 Ended");
        }
    }
}

Output: The output may vary when you run the application.

Join Method of Thread class in C# with Examples

As you can see from the above output, the Main thread is not waiting for all the child threads to complete their execution or task. If you want that the Main thread should not be existed until and unless all the child thread completes their task then you need to use the Join method which is available in Thread class.

Join Method of Thread class in C#:

The Join method of Thread class in C# blocks the current thread and makes it wait until the child thread on which the Join method invoked completes its execution. There are three overloaded versions available for the Join Method in Thread class as shown below.

Join Method of Thread class in C# with Examples

The first version of the Join method which does not take any parameter will block the calling thread (i.e. the Parent thread) until the thread (child thread) completes its execution. In this case, the calling thread is going to wait for indefinitely time until the thread on which the Join Method invoked is completed.

The second version of the Join Method allows us to specify the time out. It means it will block the calling thread until the child thread terminates or the specified time elapses. This overloaded takes the time in milliseconds. This method returns true if the thread has terminated and returns false if the thread has not terminated after the amount of time specified by the millisecondsTimeout parameter has elapsed.

The third overloaded version of this method is the same as the second overloaded version. The only difference is that here we need to use the TimeSpan to set the amount of time to wait for the thread to terminate.

Example: Using the Join Method of Thread class in C#
using System.Threading;
using System;
namespace ThreadingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main Thread Started");

            //Main Thread creating three child threads
            Thread thread1 = new Thread(Method1);
            Thread thread2 = new Thread(Method2);
            Thread thread3 = new Thread(Method3);

            thread1.Start();
            thread2.Start();
            thread3.Start();

            thread1.Join();
            thread2.Join();
            thread3.Join();

            Console.WriteLine("Main Thread Ended");
            Console.Read();
        }

        static void Method1()
        {
            Console.WriteLine("Method1 - Thread1 Started");
            Thread.Sleep(1000);
            Console.WriteLine("Method1 - Thread 1 Ended");
        }

        static void Method2()
        {
            Console.WriteLine("Method2 - Thread2 Started");
            Thread.Sleep(2000);
            Console.WriteLine("Method2 - Thread2 Ended");
        }

        static void Method3()
        {
            Console.WriteLine("Method3 - Thread3 Started");
            Thread.Sleep(5000);
            Console.WriteLine("Method3 - Thread3 Ended");
        }
    }
}

Output:

Now, for example, if you don’t want the main thread to wait until the thread3 completes its execution. Then you just need to call the Join method on thread1 and thread2.

Second Overloaded version of Join Method:

You need to use the second overloaded version when you want the main thread to wait for a specified amount of time. For example, you want the main thread to wait for 3 seconds for the thread3 to complete its task. Then you need to the Join method as shown below.

using System.Threading;
using System;
namespace ThreadingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main Thread Started");

            //Main Thread creating three child threads
            Thread thread1 = new Thread(Method1);
            Thread thread2 = new Thread(Method2);
            Thread thread3 = new Thread(Method3);

            thread1.Start();
            thread2.Start();
            thread3.Start();

            if(thread3.Join(3000))
            {
                Console.WriteLine("Thread 3 Execution Completed in 3 second");
            }
            else
            {
                Console.WriteLine("Thread 3 Execution Not Completed in 3 second");
            }

            Console.WriteLine("Main Thread Ended");
            Console.Read();
        }

        static void Method1()
        {
            Console.WriteLine("Method1 - Thread1 Started");
            Thread.Sleep(1000);
            Console.WriteLine("Method1 - Thread 1 Ended");
        }

        static void Method2()
        {
            Console.WriteLine("Method2 - Thread2 Started");
            Thread.Sleep(2000);
            Console.WriteLine("Method2 - Thread2 Ended");
        }

        static void Method3()
        {
            Console.WriteLine("Method3 - Thread3 Started");
            Thread.Sleep(5000);
            Console.WriteLine("Method3 - Thread3 Ended");
        }
    }
}

Output:

Join Method Example of Thread Class in C#

IsAlive Method of Thread Class:

The IsAlive method of Thread class returns true if the thread is still executing else returns false. Let us understand this with an example.

using System.Threading;
using System;
namespace ThreadingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main Thread Started");
            
            Thread thread1 = new Thread(Method1);     
            thread1.Start();
            
            if (thread1.IsAlive)
            {
                Console.WriteLine("Thread1 Method1 is still doing its work");
            }
            else
            {
                Console.WriteLine("Thread1 Method1 Completed its work");
            }

            thread1.Join();
            Console.WriteLine("Main Thread Ended");
            Console.Read();
        }

        static void Method1()
        {
            Console.WriteLine("Method1 - Thread1 Started");
            Console.WriteLine("Method1 - Thread 1 Ended");
        }
    }
}

Output:

Thread Class Join Method in C#

In the next article, I am going to discuss how to protect the shared resources in a multithreading environment using locking from concurrent access with example. In this article, I try to explain how to use the IsAlive and Join method of Thread class in C# using some examples.

2 thoughts on “IsAlive and Join Method of Thread class in C# ”

  1. The whole tutorial is very nice and informative
    Now under Second Overloaded version of Join Method section you have asked the main thread to wait for 3 second for thread3 to complete its task
    Here the typing error in displaying the message in the following block
    if(thread3.Join(3000))
    {
    Console.WriteLine(“Thread 3 Execution Completed in 1 second”);//it should be completed in 3 seconds not 1 second
    }
    else
    {
    Console.WriteLine(“Thread 3 Execution Not Completed in 1 second”);////it should be Not completed in 3 seconds not 1 second
    }
    Please correct me if my understanding was wrong
    Thank You

Leave a Reply

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