Lifecycle and States of a Thread in C#

30 Sep, 2021
A thread in C# at any point of time exists in any one of the following states. A thread lies only in one of the shown states at any instant:
Flow Chart: 
 
Life Cycle of a thread
 
Unstarted state: When an instance of a Thread class is created, it is in the unstarted state, means the thread has not yet started to run when the thread is in this state. Or in other words Start() method is not called. 
Unstarted state: When an instance of a Thread class is created, it is in the unstarted state, means the thread has not yet started to run when the thread is in this state. Or in other words Start() method is not called. 
Thread thr = new Thread(); 
Here, thr is at unstarted state.Runnable State: A thread that is ready to run is moved to runnable state. In this state, a thread might actually 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. Or in other words, the Start() method is called.  Running State: A thread that is running. Or in other words, the thread gets the processor.Not Runnable State: A thread that is not executable becauseSleep() method is called.Wait() method is called.Due to I/O request.Suspend() method is called.Dead State: When the thread completes its task, then thread enters into dead, terminates, abort state.
Here, thr is at unstarted state.
Runnable State: A thread that is ready to run is moved to runnable state. In this state, a thread might actually 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. Or in other words, the Start() method is called.  
Running State: A thread that is running. Or in other words, the thread gets the processor.
Not Runnable State: A thread that is not executable becauseSleep() method is called.Wait() method is called.Due to I/O request.Suspend() method is called.
Sleep() method is called.
Wait() method is called.
Due to I/O request.
Suspend() method is called.
Dead State: When the thread completes its task, then thread enters into dead, terminates, abort state.
 
Implementing Thread States in C#
In C#, to get the current state of the thread, use ThreadState or IsAlive property provided by the Thread class.Syntax: 
public ThreadState ThreadState{ get; }
OR 
public bool IsAlive { get; }
Thread class provides different types of methods to implement the states of the threads.  
Sleep() method is used to temporarily suspend the current execution of the thread for specified milliseconds, so that other threads can get the chance to start the execution, or may get the CPU for execution.
Join() method is used to make all the calling thread to wait until the main thread, i.e. joined thread complete its work.
Abort() method is used to abort the thread.
Suspend() method is called to suspend the thread.
Resume() method is called to resume the suspended thread.
Start() method is used to send a thread into runnable State.
Example: 
C#
// C# program to illustrate the// states of the threadusing System;using System.Threading; public class MyThread {     // Non-Static method    public void thread()    {        for (int x = 0; x < 2; x++) {            Console.WriteLine("My Thread");        }    }} public class ThreadExample {     // Main method    public static void Main()    {         // Creating instance for        // mythread() method        MyThread obj = new MyThread();         // Creating and initializing        // threads Unstarted state        Thread thr1 = new Thread(new ThreadStart(obj.thread));         Console.WriteLine("ThreadState: {0}",                          thr1.ThreadState);         // Running state        thr1.Start();        Console.WriteLine("ThreadState: {0}",                           thr1.ThreadState);         // thr1 is in suspended state        thr1.Suspend();        Console.WriteLine("ThreadState: {0}",                           thr1.ThreadState);         // thr1 is resume to running state        thr1.Resume();        Console.WriteLine("ThreadState: {0}",                          thr1.ThreadState);    }}
Output:  
ThreadState: Unstarted
ThreadState: Running
ThreadState: SuspendRequested
ThreadState: Running
My Thread
My Thread
Explanation: The above example shows the different states of thr1 thread. These states of a thr1 thread are determined by using the ThreadState property of the Thread class. Also, we use Suspend() and Resume() method to suspend the current execution of the thread and resume the suspended thread by using the Resume method. 
gulshankumarar231
ruhelaa48
CSharp Multithreading
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.