Open In App

How to check whether a thread is a background thread or not in C#

As we know that thread is created and managed by the Thread class. So, the Thread class provides a property known as IsBackground property to check whether the given thread is running in the background or in the foreground. If the value of IsBackground is set to be true, then it means the thread is a background thread. Or if the value of IsBackground is set to be false, then it means the thread is a foreground thread.

Syntax:



public bool IsBackground { get; set; }

Return Value: This property returns true if this thread is or is to become a background thread otherwise it returns, false.

Exception: This property will give ThreadStateException if the thread is dead.



Example 1:




// C# program to illustrate the
// concept of Background thread
using System;
using System.Threading;
  
class GFG {
  
    // Main method
    static void Main(string[] args)
    {
        // Creating and initializing thread
        Thread mythr = new Thread(mythread);
  
        // Name of the thread is Geek thread
        mythr.Name = "Geek thread";
        mythr.Start();
  
        // IsBackground is the property
        // of Thread which allows thread
        // to run in the background
        mythr.IsBackground = true;
  
        Console.WriteLine("Main Thread Ends!!");
    }
  
    // Static method
    static void mythread()
    {
  
        // Display the name of the
        // current working thread
        Console.WriteLine("In progress thread is: {0}",
                          Thread.CurrentThread.Name);
  
        Thread.Sleep(1000);
  
        Console.WriteLine("Completed thread is: {0}",
                          Thread.CurrentThread.Name);
    }
}

Output :

In progress thread is: Geek thread
Main Thread Ends!!

Example 2:




// C# program to illustrate the
// concept of IsBackground property
using System;
using System.Threading;
  
class Geeks {
  
    int J;
  
    public Geeks(int J)
    {
        this.J = J;
    }
  
    // This method will display
    // the counting of background
    // and foreground threads
    public void display()
    {
        for (int i = 0; i < J; i++) {
              
            Console.WriteLine("{0} count: {1}"
             Thread.CurrentThread.IsBackground ? 
                           "Background Thread"
                        "Foreground Thread", i);
                          
            Thread.Sleep(250);
        }
          
        Console.WriteLine("{0} finished its counting.",
                    Thread.CurrentThread.IsBackground ?
            "Background Thread" : "Foreground Thread");
    }
}
  
// Driver Class
class GFG {
      
    // Main method
    static void Main()
    {
        // Creating and initializing
        // objects of Geeks class
        // Creating and initializing 
        // objects of Thread class
        Geeks obj1 = new Geeks(5);
        Thread fthr = new Thread(new ThreadStart(obj1.display));
  
        Geeks obj2 = new Geeks(10);
        Thread bthr = new Thread(new ThreadStart(obj2.display));
  
        // bthr thread is a background
        // thread because the value of
        // IsBackground property is true
        bthr.IsBackground = true;
  
        fthr.Start();
        bthr.Start();
    }
}

Output :

Foreground Thread count: 0
Background Thread count: 0
Background Thread count: 1
Foreground Thread count: 1
Background Thread count: 2
Foreground Thread count: 2
Background Thread count: 3
Foreground Thread count: 3
Background Thread count: 4
Foreground Thread count: 4
Background Thread count: 5
Foreground Thread finished its counting.

Note:

Reference:


Article Tags :
C#