Open In App

Lifecycle and States of a Thread in C#

Improve
Improve
Like Article
Like
Save
Share
Report

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

 

  1. 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(); 
  1. Here, thr is at unstarted state.
  2. 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. 
     
  3. Running State: A thread that is running. Or in other words, the thread gets the processor.
  4. Not Runnable State: A thread that is not executable because
    • Sleep() method is called.
    • Wait() method is called.
    • Due to I/O request.
    • Suspend() method is called.
  5. 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 thread
using 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.
 



Last Updated : 30 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads