Thread.IsBackground Property
Gets or sets a value indicating whether or not a thread is a background thread.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| ThreadStateException |
The thread is dead. |
A thread is either a background thread or a foreground thread. Background threads are identical 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 following code example contrasts the behavior of foreground and background threads. A foreground thread and a background thread are created. The foreground thread keeps the process running until it completes its while loop. After the foreground thread has finished, the process is terminated before the background thread has completed its while loop.
using System; using System.Threading; class Test { static void Main() { BackgroundTest shortTest = new BackgroundTest(10); Thread foregroundThread = new Thread(new ThreadStart(shortTest.RunLoop)); foregroundThread.Name = "ForegroundThread"; BackgroundTest longTest = new BackgroundTest(50); Thread backgroundThread = new Thread(new ThreadStart(longTest.RunLoop)); backgroundThread.Name = "BackgroundThread"; backgroundThread.IsBackground = true; foregroundThread.Start(); backgroundThread.Start(); } } class BackgroundTest { int maxIterations; public BackgroundTest(int maxIterations) { this.maxIterations = maxIterations; } public void RunLoop() { String threadName = Thread.CurrentThread.Name; for(int i = 0; i < maxIterations; i++) { Console.WriteLine("{0} count: {1}", threadName, i.ToString()); Thread.Sleep(250); } Console.WriteLine("{0} finished counting.", threadName); } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
There is no information about default value of this property.
The default is False
Thx for pointing that out. By default, a newly created thread is a foreground thread, so IsBackground is False.
Glenn Hackney
Common Language Runtime Developer Guidance
- 1/6/2012
- godzy
- 2/3/2012
- Glenn Hackney - MSFT
- 10/6/2011
- send me teh codez
- 12/21/2011
- Vin Owen
- 8/21/2010
- Neelam Rajkumar Reddy
[gh] No, setting IsBackground to false makes the thread a foreground thread again. I just tried this, and the thread kept the process alive. If you have a concise repro, use Click to Rate and Give Feedback to send it to me (a bug will be created with my name on it :-). Or if you have the MSDN documentation installed on your computer, click the Send Feedback link in this article and send it that way; again, a bug will be created automatically. -- Glenn
- 4/20/2010
- leabre
- 4/29/2010
- Glenn Hackney - MSFT
