Thread.IsBackground Property

May 11, 2014

Gets or sets a value that indicates whether a thread is a background thread.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public bool IsBackground { get; set; }

Property Value

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

ExceptionCondition
ThreadStateException

Execution of the thread has terminated.

A thread is either a background thread or a foreground thread. In Windows Phone, there is no difference in behavior between the two.

The following example shows how to start foreground and background threads. The example creates two threads by using the Thread(ParameterizedThreadStart) constructor. One is run as a foreground thread (the default) and the other is changed to a background thread by setting its IsBackground property to true. The example also starts a thread pool thread by calling the QueueUserWorkItem method. Thread pool threads are background threads.

All three threads display their IsBackground property values.


using System;
using System.Threading;
using System.Windows.Input;

public class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;
      outputBlock.Text = "Click here to begin the demo.\n";

      outputBlock.MouseLeftButtonUp += new MouseButtonEventHandler(MouseUp);
   }

   private static void MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
   {
      outputBlock.MouseLeftButtonUp -= new MouseButtonEventHandler(MouseUp);
      outputBlock.Text = "To run the demo again, refresh the page.\n\n";

      Thread backgroundThread = new Thread(RunLoop);
      backgroundThread.Name = "BackgroundThread";
      backgroundThread.IsBackground = true;
      backgroundThread.Start(null);

      Thread foregroundThread = new Thread(RunLoop);
      foregroundThread.Name = "ForegroundThread";
      foregroundThread.Start(null);

      ThreadPool.QueueUserWorkItem(RunLoop);
   }

   private static void RunLoop(object state)
   {
      string threadName = Thread.CurrentThread.Name;
      if (threadName==null) {threadName = "<ThreadPool thread>";}

      outputBlock.Dispatcher.BeginInvoke(displayHelper, 
         String.Format("{0} is running. IsBackground = {1}\n", 
                       threadName, Thread.CurrentThread.IsBackground));
   }

   // In order to update the TextBlock object, which is on the UI thread, you must
   // make a cross-thread call by using the Dispatcher object that is associated 
   // with the TextBlock. The DisplayOutput helper method and its delegate, 
   // displayHelper, are used by the BeginInvoke method of the Dispatcher object
   // to append text to the TextBlock. 
   //
   private static Action<string> displayHelper = new Action<string>(DisplayOutput);
   private static void DisplayOutput(string msg)
   {
      outputBlock.Text += msg;
   }
}

/* This example produces output similar to the following:

BackgroundThread is running. IsBackground = True.
ForegroundThread is running. IsBackground = False.
<ThreadPool thread> is running. IsBackground = True.
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft