Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check whether a thread is alive or not in C#
To check whether a thread is alive or not in C#, you use the IsAlive property of the Thread class. This property returns true if the thread has been started and has not yet terminated normally or been aborted.
Syntax
Following is the syntax for checking if a thread is alive −
bool isAlive = thread.IsAlive;
The IsAlive property returns true when the thread is in Running, WaitSleepJoin, or Suspended states, and false when the thread is in Unstarted, Stopped, or Aborted states.
Using IsAlive with Current Thread
Example
using System;
using System.Threading;
public class Demo {
public static void Main() {
Thread thread = new Thread(new ThreadStart(demo1));
thread = Thread.CurrentThread;
Console.WriteLine("Is the thread alive? = " + thread.IsAlive);
ThreadPool.QueueUserWorkItem(new WaitCallback(demo2));
Console.WriteLine("Current state of Thread = " + thread.ThreadState);
Console.WriteLine("ManagedThreadId = " + thread.ManagedThreadId);
thread.IsBackground = true;
Console.WriteLine("Is the Thread a background thread? = " + thread.IsBackground);
}
public static void demo1() {
Thread.Sleep(2000);
}
public static void demo2(object stateInfo) {
Console.WriteLine("Thread belongs to managed thread pool? = " + Thread.CurrentThread.IsThreadPoolThread);
}
}
The output of the above code is −
Is the thread alive? = True Current state of Thread = Running ManagedThreadId = 1 Is the Thread a background thread? = True Thread belongs to managed thread pool? = True
Using IsAlive with Started Thread
Example
using System;
using System.Threading;
public class Demo {
public static void Main() {
Thread thread = new Thread(new ThreadStart(demo));
thread.Start();
Console.WriteLine("Is the Thread alive? " + thread.IsAlive);
}
public static void demo() {
Console.WriteLine("Thread belongs to managed thread pool? = " + Thread.CurrentThread.IsThreadPoolThread);
}
}
The output of the above code is −
Is the Thread alive? True Thread belongs to managed thread pool? = False
Checking Thread State Changes
Example
using System;
using System.Threading;
public class Demo {
public static void Main() {
Thread thread = new Thread(new ThreadStart(WorkerMethod));
Console.WriteLine("Before Start - IsAlive: " + thread.IsAlive);
Console.WriteLine("Before Start - ThreadState: " + thread.ThreadState);
thread.Start();
Console.WriteLine("After Start - IsAlive: " + thread.IsAlive);
Console.WriteLine("After Start - ThreadState: " + thread.ThreadState);
thread.Join(); // Wait for thread to complete
Console.WriteLine("After Join - IsAlive: " + thread.IsAlive);
Console.WriteLine("After Join - ThreadState: " + thread.ThreadState);
}
public static void WorkerMethod() {
Console.WriteLine("Worker thread executing...");
Thread.Sleep(1000);
Console.WriteLine("Worker thread completed.");
}
}
The output of the above code is −
Before Start - IsAlive: False Before Start - ThreadState: Unstarted After Start - IsAlive: True After Start - ThreadState: Running Worker thread executing... Worker thread completed. After Join - IsAlive: False After Join - ThreadState: Stopped
Conclusion
The IsAlive property in C# provides a simple way to check if a thread is currently active. It returns true for running, waiting, or suspended threads, and false for unstarted or completed threads. This property is useful for thread monitoring and synchronization scenarios.
