Last Updated : 11 Jul, 2025
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);
}
}
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 Propertypublic 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);
}
}
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,
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4