Open In App

How to Check Whether a Thread is Alive or Not in C#?

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as IsAlive to check if the thread is alive or not. In other words, the value of this property indicates the current execution of the thread.

Example 1: This example demonstrates how to check the state of the main thread using the IsAlive property.

C#
// C# program to illustrate the 
// use of IsAlive property 
using System;
using System.Threading;

public class Geeks
{
    static public void Main()
    {
        Thread t;

        // Get the reference of main thread 
        // using CurrentThread property
        t = Thread.CurrentThread;

        // Display the current state of the 
        // main thread using IsAlive property
        Console.WriteLine("Is the main thread alive? : {0}", t.IsAlive);
    }
}

Output
Is the main thread alive? : True

Explanation: In this example, we check the state of the main thread using Thread.CurrentThread. The IsAlive property returns true because the main thread is alive as long as the program is running.

Syntax of IsAlive Property

public bool IsAlive { get; }

Return Type: This property returns "true" if the thread is started and not terminated normally or aborted, otherwise, return "false". The return type of this property is bool (boolean).

Example 2: This example demonstrates how to check whether custom threads (not the main thread) are alive by using the IsAlive property.

C#
// C# program to illustrate the 
// use of IsAlive property 
using System;
using System.Threading;

public class Geeks
{
	public static void Main()
	{
		// Creating and initializing threads 
		Thread T1 = new Thread(new ThreadStart(job));
		Thread T2 = new Thread(new ThreadStart(job));

		// Display the current state of 
		// the threads Using IsAlive property 	
		Console.WriteLine("Is thread 1 is alive : {0}",
		T1.IsAlive);

		Console.WriteLine("Is thread 2 is alive : {0}",
		T2.IsAlive);

		T1.Start();
		T2.Start();

		// Display the current state of 
		// the threads Using IsAlive property  
		Console.WriteLine("Is thread 1 is alive : {0}",
		T1.IsAlive);

		Console.WriteLine("Is thread 2 is alive : {0}",
		T2.IsAlive);
	}
	public static void job()
	{
		Thread.Sleep(200);
	}
}

Output
Is thread 1 is alive : False
Is thread 2 is alive : False
Is thread 1 is alive : True
Is thread 2 is alive : True

Explanation: In the above example,

  • Before the threads are started, both T1.IsAlive and T2.IsAlive return false because the threads have not started yet.
  • After calling Start(), the threads begin executing. However, the IsAlive check immediately after starting might still return false due to timing and thread scheduling.
  • After the Thread.Sleep(500), both threads should be alive, so IsAlive returns true for both threads.

Note: If you want to ensure the threads have completed their execution before checking their status again, you could use T1.Join() and T2.Join() to block the main thread until both threads finish executing.


Next Article

Similar Reads