Open In App
Related Articles

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

Improve Article
Save Article
Like Article

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.


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";
        // 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}",
        Console.WriteLine("Completed thread is: {0}",

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

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.


  • A thread is either a background thread or a foreground thread. Background threads are similar to foreground threads, except that background threads do not prevent a process from terminating.
  • Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.
  • The primary thread or main application thread and all the threads created by the Thread class constructor execute in the foreground(i.e. their IsBackground property returns false).
  • Thread pool threads and all the threads that enter the managed execution environment from unmanaged code execute in the background (i.e. their IsBackground property returns true).


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 24 Jan, 2019
Like Article
Save Article
Similar Reads