Open In App

C# Lifecycle and States of a Thread

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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(milliseconds) is called.
  • Thread.Join() is called, making another thread wait for completion.
  • It is waiting for a lock or I/O operation.

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.

thread.Join(); // Waits for the thread to finish execution

Console.WriteLine(thread.ThreadState); // Output: Stopped

Hierarchy Diagram of Thread States

LifeCycleOfThread


Implementing Thread States

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 Property

ThreadState 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.

public ThreadState ThreadState{ get; }

2. IsAlive Property

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.

public bool IsAlive { get; }

Methods for Managing Thread States

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}"); 
    }
}

Output
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,

  • Initially "t" thread was Unstarted.
  • After call start() transitions to the Runnable state and eventually enters the Running state when it gets CPU time.
  • During the execution of Thread.Sleep in ThreadMethod, the thread enters the Not Runnable state (shown as WaitSleepJoin).
  • After the thread completes its task, it moves to the Dead state, which is confirmed by calling t.Join() and check its final state.

Article Tags :

Similar Reads