This documentation is archived and is not being maintained.

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:
property bool IsBackground {
	bool get ();
	void set (bool value);
}

Property Value

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

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



.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

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.
Show: