Thread.IsBackground Property (System.Threading)

Switch View :
ScriptFree
.NET Framework Class Library
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)
Syntax

Visual Basic
Public Property IsBackground As Boolean
C#
public bool IsBackground { get; set; }
Visual C++
public:
property bool IsBackground {
	bool get ();
	void set (bool value);
}
F#
member IsBackground : bool with get, set

Property Value

Type: System.Boolean
true if this thread is or is to become a background thread; otherwise, false.
Exceptions

Exception Condition
ThreadStateException

The thread is dead.

Remarks

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.

Examples

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.

Visual Basic

Imports System
Imports System.Threading

Public Class Test

    <MTAThread> _
    Shared Sub Main()
        Dim shortTest As New BackgroundTest(10)
        Dim foregroundThread As New Thread(AddressOf shortTest.RunLoop)
        foregroundThread.Name = "ForegroundThread"

        Dim longTest As New BackgroundTest(50)
        Dim backgroundThread As New Thread(AddressOf longTest.RunLoop)
        backgroundThread.Name = "BackgroundThread"
        backgroundThread.IsBackground = True

        foregroundThread.Start()
        backgroundThread.Start()
    End Sub

End Class

Public Class BackgroundTest

    Dim maxIterations As Integer 

    Sub New(maximumIterations As Integer)
        maxIterations = maximumIterations
    End Sub

    Sub RunLoop()
        Dim threadName As String = Thread.CurrentThread.Name

        For i As Integer = 0 To maxIterations
            Console.WriteLine("{0} count: {1}", _
                    threadName, i.ToString())
            Thread.Sleep(250)
        Next i

        Console.WriteLine("{0} finished counting.", threadName)
    End Sub

End Class


C#

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


Visual C++

using namespace System;
using namespace System::Threading;
ref class BackgroundTest
{
private:
   int maxIterations;

public:
   BackgroundTest( int maxIterations )
   {
      this->maxIterations = maxIterations;
   }

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

};

int main()
{
   BackgroundTest^ shortTest = gcnew BackgroundTest( 10 );
   Thread^ foregroundThread = gcnew Thread( gcnew ThreadStart( shortTest, &BackgroundTest::RunLoop ) );
   foregroundThread->Name =  "ForegroundThread";
   BackgroundTest^ longTest = gcnew BackgroundTest( 50 );
   Thread^ backgroundThread = gcnew Thread( gcnew ThreadStart( longTest, &BackgroundTest::RunLoop ) );
   backgroundThread->Name =  "BackgroundThread";
   backgroundThread->IsBackground = true;
   foregroundThread->Start();
   backgroundThread->Start();
}



Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library
Platforms

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.
See Also

Reference

Other Resources

Community Content

Glenn Hackney - MSFT
default value

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


Vin Owen
To Neelam: That behavior is 100% correct.
Background threads are stopped when your application closes.  Until you close your form, your application is still running, so the CLR will not terminate background threads until then. Hippyy ya

Neelam Rajkumar Reddy
Background thread is not getting terminated in windows application (forms)
I executed this program in windows application and i noticed that the background thread is not getting terminated. i can still see the output returned by background thread.  i only can't see the output of background thread when i close the form. i think, the background thread is getting terminated by CLR only when i close the form.

Glenn Hackney - MSFT
Clarification
The documentation states that this property gets or sets a value indicating whether the thread is background or not. It is unclear if these are set-once semantics. I'm observing that if a thread is ever at any point in its lifetime, however brief, set to IsBackground=true, even if set back to false on the next line of code, it permanently becomes a background thread even if the proper reads contrary, and will not prevent a process from exiting before it completes its work. Is the behavior supposed to be such that once IsBackground has been set, it cannot be unset?

[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