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:
- 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).
Reference:
Recommended Posts:
- C# | Check if a thread belongs to managed thread pool or not
- How to check whether a thread is alive or not in C#
- Naming a thread and fetching name of current thread in C#
- C# | Check if an array is synchronized (thread safe) or not
- C# | Check whether a Hashtable contains a specific key or not
- C# | Check if StringCollection is synchronized (thread safe)
- C# | Check if the BitArray is synchronized (thread safe)
- C# | Check if StringDictionary is synchronized (thread safe)
- C# | Check if ListDictionary is synchronized (thread safe)
- C# | Check if HybridDictionary is Synchronized (thread safe)
- C# | Check if Hashtable is synchronized (thread safe)
- C# | Check if ArrayList is Synchronized (thread safe)
- C# | How to check current state of a thread
- C# | Creating a synchronized (thread-safe) wrapper for a SortedList object
- Thread.CurrentThread Property in C#
- C# | Creating a synchronized (thread-safe) wrapper for the Hashtable
- C# | Creating a synchronized (thread-safe) wrapper for the ArrayList
- C# | Thread Priority in Multithreading
- Lifecycle and States of a Thread in C#
- Main Thread in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

