Threads in C#

A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time-consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.

The life cycle of a thread starts when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution.

The following are the various states in the life cycle of a thread −

  • The Unstarted State - It is the situation when the instance of the thread is created but the Start method is not called.

  • The Ready State - It is the situation when the thread is ready to run and waiting CPU cycle.

  • The Not Runnable State - A thread is not executable, when

    • Sleep method has been called
    • Wait method has been called
    • Blocked by I/O operations
  • The Dead State - It is the situation when the thread completes execution or is aborted.

The following is an example showing how to create a thread −


 Live Demo

using System;
using System.Threading;

namespace Demo {
   class Program {
      public static void ThreadFunc() {
         Console.WriteLine("Child thread starts");

      static void Main(string[] args) {
         ThreadStart childref = new ThreadStart(ThreadFunc);
         Console.WriteLine("In Main: Creating the Child thread");
         Thread childThread = new Thread(childref);


In Main: Creating the Child thread
Child thread starts
Samual Sam
Samual Sam

Learning faster. Every day.