IsBackground Property

Thread.IsBackground Property


Gets or sets a value indicating whether or not a thread is a background thread.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public bool IsBackground {
	[HostProtectionAttribute(SecurityAction.LinkDemand, SelfAffectingThreading = true)]

Property Value

Type: System.Boolean

true if this thread is or is to become a background thread; otherwise, false.

Exception Condition

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;


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());
        Console.WriteLine("{0} finished counting.", threadName);

.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2015 Microsoft