Last Updated : 11 Jul, 2025
In C#, threads enable the concurrent execution of tasks, it improves application performance and responsiveness. Understanding the lifecycle and states of a thread is essential for managing and synchronizing threads efficiently. A thread in C# can exist in one of the following states at any given time.
States of a Thread in C#1. Unstarted state: When an instance of a Thread class is created, it is unstarted, which means the thread has not yet started to run when the thread is in this state. In other words, the Start() method is not called.
Thread thread = new Thread(SomeMethod); // Thread is created but not started
Console.WriteLine(thread.ThreadState); // Output: Unstarted
2. Runnable State: A thread that is ready to run is moved to runnable state. In this state, a thread might be running or it might be ready to run at any instant of time. It is the responsibility of the thread scheduler to give the thread, time to run. In other words, the Start() method is called.
3. Running State: A thread moves to the Running state after calling Start() and when the CPU scheduler assigns execution time.
thread.Start();
Console.WriteLine(thread.ThreadState); // Output: Running (if executing)
4. WaitSleepJoin State: A thread enters this state when:
Thread.Sleep(1000);
Console.WriteLine(thread.ThreadState); // Output: WaitSleepJoin
5. Dead State: When the thread completes its task, the thread enters into dead, terminates, abort state.
Hierarchy Diagram of Thread States Implementing Thread Statesthread.Join(); // Waits for the thread to finish execution
Console.WriteLine(thread.ThreadState); // Output: Stopped
ThreadState and IsAlive properties of the Thread class provide the necessary tools to determine and manage the state of a thread. To get the current state of the thread, use the ThreadState or IsAlive property provided by the Thread class in C#. These properties are described below:
1. ThreadState PropertyThreadState property is a read-only property of the Thread class, which provides the current state of a thread. It returns a value from the ThreadState enum that represents the state of the thread at that instant.
2. IsAlive Propertypublic ThreadState ThreadState{ get; }
IsAlive property is used to check whether the thread is currently alive or has completed its execution. This property returns true if the thread has been started and is not in the Dead state, and false if the thread has finished execution or has not been started.
Methods for Managing Thread Statespublic bool IsAlive { get; }
The thread class provides different types of methods to implement the states of the threads.
Method
Description
Sleep()
Temporarily suspend the current execution of the thread for some milliseconds, to allow other threads to execute or access the CPU
Join()
Use to make all the calling threads wait until the main thread, i.e. joined thread completes its work.
Abort()
Use to abort the thread.
Suspend()
Use it to suspend the thread.
Resume()
Called to resume the suspended thread.
Start()
Use to Start() method is used.
Note: Thread.Resume() and Thread.Suspend() methods are deprecated no longer used in newer versions instead of this we can use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore.
Example: Demonstrating Different Thread States
C#
// C# Program to demonstrate different
// thread states
using System;
using System.Threading;
class Geeks
{
static void ThreadMethod()
{
Thread.Sleep(500);
Console.WriteLine("Thread completed its task.");
}
public static void Main()
{
// Create a thread
Thread t = new Thread(ThreadMethod);
Console.WriteLine($"Thread State (Before Start): {t.ThreadState}");
// Start the thread (Runnable state)
t.Start();
Console.WriteLine($"Thread State (After Start): {t.ThreadState}");
// Simulate Not Runnable state
Thread.Sleep(200);
// Ensure the thread is running
Console.WriteLine($"Thread State (During Sleep): {t.ThreadState}");
// Wait for the thread to complete (Dead state)
t.Join();
Console.WriteLine($"Thread State (After Join): {t.ThreadState}");
}
}
Thread State (Before Start): Unstarted Thread State (After Start): Running Thread State (During Sleep): WaitSleepJoin Thread completed its task. Thread State (After Join): Stopped
Explanation: In the above example,
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