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)

property bool IsBackground {
	bool get();
	[HostProtectionAttribute(SecurityAction::LinkDemand, SelfAffectingThreading = true)]
	void set(bool value);

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.

By default, the following threads execute in the foreground (that is, their IsBackground property returns false):

  • The primary thread (or main application thread).

  • All threads created by calling a Thread class constructor.

By default, the following threads execute in the background (that is, their IsBackground property returns true):

  • Thread pool threads, which are a pool of worker threads maintained by the runtime. You can configure the thread pool and schedule work on thread pool threads by using the ThreadPool class.


    Task-based asynchronous operations automatically execute on thread pool threads.

  • All threads that enter the managed execution environment from unmanaged code.

The following example contrasts the behavior of foreground and background threads. It creates a foreground thread and a background thread. The foreground thread keeps the process running until completes its for loop and terminates. However, as the output from the example shows, because the foreground thread has finished execution, the process is terminated before the background thread has completed execution.

using namespace System;
using namespace System::Threading;

ref class BackgroundTest
   int maxIterations;

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

   void RunLoop()
      for (int i = 0; i < maxIterations; i++ )
         Console::WriteLine("{0} count: {1}", 
              Thread::CurrentThread->IsBackground ? 
              "Background Thread" : "Foreground Thread", i);

      Console::WriteLine("{0} finished counting.", 
                         Thread::CurrentThread->IsBackground ? 
                         "Background Thread" : "Foreground Thread");

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;

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