IsAlive Property and Join Method of Thread Class in C# 

IsAlive Property and Join Method of Thread Class in C# with Examples

In this article, I am going to discuss the use of IsAlive Property and Join Method of Thread Class in C# with Examples. Please read our previous article where we discussed How to Return Data from a Thread Function in C# using the Callback method with an Example. As part of this article, we are going to discuss the following pointers.

  1. Understanding the need for Join Method of Thread Class in C#.
  2. Examples using different overloaded versions of the Join Method.
  3. Examples to Understand the use of IsAlive Property of Thread Class in C#.
Understanding the need for Join Method of Thread Class in C#.

Let us understand the use of the Join Method of Thread Class in C# with Examples. For a better understanding, please have a look at the following example. In the below example we have created three methods and then execute these three methods using three separate threads. The point that you need to remember is the threads thread1, thread2, and thread3 are called the child threads of the main thread. This is because these three threads are created by the main thread only.

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 the Main thread should not be exited until and unless all the child thread completes their task then you need to use the Join method of the Thread class in C#.

Join Method of Thread Class in C#:

The Join Method of the 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

  1. Join(): This method blocks the calling thread until the thread represented by this instance terminates while continuing to perform standard COM and SendMessage pumping. It will throw ThreadStateException if the caller attempted to join a thread that is in the System.Threading.ThreadState.Unstarted state.
  2. Join(int millisecondsTimeout): 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. The parameter millisecondsTimeout specifies the number of milliseconds to wait for the thread to terminate. It returns true if the thread has terminated; false if the thread has not terminated after the amount of time specified by the millisecondsTimeout parameter has elapsed. It will throw ArgumentOutOfRangeException if the value of millisecondsTimeout is negative and is not equal to System.Threading.Timeout.Infinite in milliseconds. It will throw ThreadStateException if the thread has not been started.
  3. Join(TimeSpan timeout): 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. Here, the parameter timeout specifies a System.TimeSpan is set to the amount of time to wait for the thread to terminate. It returns true if the thread terminated; false if the thread has not terminated after the amount of time specified by the timeout parameter has elapsed. It throws ArgumentOutOfRangeException if the value of timeout is negative and is not equal to System.Threading.Timeout.Infinite in milliseconds, or is greater than System.Int32.MaxValue milliseconds. It throws ThreadStateException if the caller attempted to join a thread that is in the System.Threading.ThreadState.Unstarted state.

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 an indefinite time until the thread on which the Join Method is 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 to Understand the Join Method of Thread Class in C#

For a better understanding of how to use the Thread Class Join Method in C#, please have a look at the below example. In the below example, we have called the Join method on all the three threads which means it will block the main thread until all the child threads complete their tasks. 

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:

Example to Understand the Join Method of Thread Class in C#

Now, for example, if you don’t want the main thread to wait until thread3 completes its execution. Then you just need to call the Join method on thread1 and thread2 as shown in the below 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();

            thread1.Join();
            thread2.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:

IsAlive Property and Join Method of Thread Class in C# with Examples

Other Overloaded Versions of Thread Class Join Method in C#:

You need to use the second overloaded version of the Thread Class Join Method in C# 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 thread3 to complete its task. Then you need to use the Join method as shown below in the below 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();

            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 of Thread Class in C# with Examples

IsAlive Property of Thread Class in C#:

The IsAlive property gets a value indicating the execution status of the current thread. It returns true if the thread has been started and has not terminated normally or aborted; otherwise, false. That means the IsAlive property of the 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:

how to use the IsAlive Property and Join Method of Thread class in C# using Examples

In the next article, I am going to discuss Thread Synchronization in C# with Examples. Here, in this article, I try to explain how to use the IsAlive Property and Join Method of Thread class in C# using Examples. I hope you enjoy this IsAlive Property and Join Method of Thread class in C# using Examples article.

4 thoughts on “IsAlive Property and Join Method of Thread Class in C# ”

  1. blank

    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

  2. blank

    As per my understanding, there is one typo in the 3nd code snippet where we are not using thread1.join() or thread2.join() and as the thread is sleeping inside these methods so it should be not completed before main thread. Please check the output.
    Or is this the case if the main thread is waiting for thread3 to finish in 3 seconds and so in meantime it will complete the thread1 and thread2

Leave a Reply

Your email address will not be published.