What is the use of IsBackground property of Thread?

In any programming language that supports multi-tasking (which is supported by most of the modern languages) Threads are the most important part (tool). We use threads to run something which does not need user interaction and which runs in parallel with the normal operations by the user. In C#, a special class is provided which creates a new parallel execution block called Thread. Thread has a number of properties that helps in configuring the new thread you have created.

You might be aware of the fact that Threads actually comprises a process. So when your application runs, it allocates a new process into memory, and a few threads are created. One of such Thread is the UI Thread which runs the whole application. Each thread has a property associated with it called IsBackground which identifies whether the Thread is running in foreground or in background. A background thread will not impose restriction to the process to terminate. Hence if you create a new thread and set IsBackground of the Thread to false, that indicates that if there is only this thread available and all the Foreground threads has terminated, please terminate the process without waiting for the Background thread to complete.
In other words, the IsBackground property indicates that this thread is not important enough to keep the process running. We generally do maintenance jobs in a Background Threads. Lets see how to define a Background Thread in your application :

Thread th = new Thread(RunMe);
th.IsBackground = true;
th.Start();
th.Join();

The Thread is executing a method called RunMe, which might end eventually. The IsBackground property of the Thread th is set to true, that means if we do not write th.Join, or block the main thread to this thread, the program will eventually end terminating the RunMe execution abruptly.
On the other hand, if you set IsBackground to false, it will keep on running even though your main thread terminates. The process will eventually terminate only when all the Foreground thread finishes its execution.

Note : Do not get confused with Thread Priority and IsBackground property. I have seen many of the developers confuse between these concepts. So beware, as there is nothing related to Timeslices shared by background and foreground threads.
I hope this helps you.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434