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 (Declaration)
Public Property IsBackground As Boolean
Visual Basic (Usage)
Dim instance As Thread
Dim value As Boolean

value = instance.IsBackground

instance.IsBackground = value
C#
public bool IsBackground { get; set; }
C++
public:
property bool IsBackground {
	bool get ();
	void set (bool value);
}
J#
/** @property */
public boolean get_IsBackground ()

/** @property */
public void set_IsBackground (boolean value)

JScript
public function get IsBackground () : boolean

public function set IsBackground (value : boolean)

Property Value

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

Exception type 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.

Example

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

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


J#
import System.*;
import System.Threading.*;
import System.Threading.Thread;

class Test
{
    public static void main(String[] args)
    {
        BackgroundTest shortTest = new BackgroundTest(10);
        Thread foregroundThread = new Thread(new ThreadStart(shortTest.RunLoop));

        foregroundThread.set_Name("ForegroundThread");

        BackgroundTest longTest = new BackgroundTest(50);
        Thread backgroundThread = new Thread(new ThreadStart(longTest.RunLoop));

        backgroundThread.set_Name("BackgroundThread");
        backgroundThread.set_IsBackground(true);
        foregroundThread.Start();
        backgroundThread.Start();
    } //main
} //Test

class BackgroundTest
{
    private int maxIterations;

    public BackgroundTest(int maxIterations)
    {
        this.maxIterations = maxIterations;
    } //BackgroundTest

    public void RunLoop()
    {
        String threadName = Thread.get_CurrentThread().get_Name();

        for (int i = 0; i < maxIterations; i++) {
            Console.WriteLine("{0} count: {1}", threadName, String.valueOf(i));
            Thread.Sleep(250);
        }

        Console.WriteLine("{0} finished counting.", threadName);
    } //RunLoop
} //BackgroundTest

Platforms

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
See Also