How to check whether a thread is alive or not in C#
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. Or in other words, the value of this property indicates the current execution of the thread.
Syntax:
public bool IsAlive { get; }
Return Value: 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 System.Boolean.
Below programs illustrate the use of IsAlive property:
Example 1:
// C# program to illustrate the // use of IsAlive property using System; using System.Threading; public class GFG { // Main Method static public void Main() { Thread thr; // Get the reference of main Thread // Using CurrentThread property thr = Thread.CurrentThread; // Display the current state of // the main thread Using IsAlive // property Console.WriteLine("Is main thread is alive"+ " ? : {0}", thr.IsAlive); } } |
Output:
Is main thread is alive ? : True
Example 2:
// C# program to illustrate the // use of IsAlive property using System; using System.Threading; public class GFG { // Main method public static void Main() { // Creating and initializing threads Thread Thr1 = new Thread(new ThreadStart(job)); Thread Thr2 = new Thread(new ThreadStart(job)); // Display the current state of // the threads Using IsAlive // property Console.WriteLine("Is thread 1 is alive : {0}", Thr1.IsAlive); Console.WriteLine("Is thread 2 is alive : {0}", Thr2.IsAlive); Thr1.Start(); Thr2.Start(); // Display the current state of // the threads Using IsAlive // property Console.WriteLine("Is thread 1 is alive : {0}", Thr1.IsAlive); Console.WriteLine("Is thread 2 is alive : {0}", Thr2.IsAlive); } // Static method public static void job() { Thread.Sleep(2000); } } |
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
Reference:
Recommended Posts:
- C# | Check if a thread belongs to managed thread pool or not
- How to check whether a thread is a background thread or not in C#
- C# | How to check current state of a thread
- C# | Check if ArrayList is Synchronized (thread safe)
- C# | Check if the BitArray is synchronized (thread safe)
- C# | Check if an array is synchronized (thread safe) or not
- C# | Check if Hashtable is synchronized (thread safe)
- C# | Check if ListDictionary is synchronized (thread safe)
- C# | Check if HybridDictionary is Synchronized (thread safe)
- C# | Check if StringCollection is synchronized (thread safe)
- C# | Check if StringDictionary is synchronized (thread safe)
- Naming a thread and fetching name of current thread in C#
- How to Terminate a Thread in C#
- Thread Class in C#
- Main Thread in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

