Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Thread.IsBackground Property

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

[Visual Basic]
Public Property IsBackground As Boolean
public bool IsBackground {get; set;}
public: __property bool get_IsBackground();
public: __property void set_IsBackground(bool);
public function get IsBackground() : Boolean;
public function set IsBackground(Boolean);

Property Value

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


Exception Type 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 by invoking Abort on any background threads that are still alive.


[Visual Basic, C#, C++] 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

    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

    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())
        Next i

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

End Class

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

#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;

__gc class BackgroundTest
    int maxIterations;

    BackgroundTest(int maxIterations)
        this->maxIterations = maxIterations;

    void RunLoop()
        String* threadName = Thread::CurrentThread->Name;
        for(int i = 0; i < maxIterations; i++)
            Console::WriteLine(S"{0} count: {1}", 
                threadName, i.ToString());
        Console::WriteLine(S"{0} finished counting.", threadName);

void main()
    BackgroundTest* shortTest = new BackgroundTest(10);
    Thread* foregroundThread = new Thread(
        new ThreadStart(shortTest, &BackgroundTest::RunLoop));
    foregroundThread->Name = "ForegroundThread";

    BackgroundTest* longTest = new BackgroundTest(50);
    Thread* backgroundThread = new Thread(
        new ThreadStart(longTest, &BackgroundTest::RunLoop));
    backgroundThread->Name = "BackgroundThread";
    backgroundThread->IsBackground = true;


[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Thread Class | Thread Members | System.Threading Namespace | Foreground and Background Threads

© 2017 Microsoft