Thread.IsBackground Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Property IsBackground As Boolean
public bool IsBackground { get; set; }

Property Value

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

Exceptions

Exception Condition
ThreadStateException

Execution of the thread has terminated.

Remarks

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

Examples

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.

Imports System.Threading

Public Class Example

   Private Shared outputBlock As System.Windows.Controls.TextBlock

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock
      outputBlock.Text = "Click here to begin the demo." & vbLf 

      AddHandler outputBlock.MouseLeftButtonUp, AddressOf MouseUp

   End Sub

   Private Shared Sub MouseUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)

      RemoveHandler outputBlock.MouseLeftButtonUp, AddressOf MouseUp
      outputBlock.Text = "To run the demo again, refresh the page." & vbLf & vbLf

      Dim backgroundThread As New Thread(AddressOf RunLoop)
      backgroundThread.Name = "BackgroundThread"
      backgroundThread.IsBackground = True
      backgroundThread.Start(Nothing)

      Dim foregroundThread As New Thread(AddressOf RunLoop)
      foregroundThread.Name = "ForegroundThread"
      foregroundThread.Start(Nothing)

      ThreadPool.QueueUserWorkItem(AddressOf RunLoop)

   End Sub

   Private Shared Sub RunLoop(ByVal State As Object)

      Dim threadName As String = Thread.CurrentThread.Name
      If threadName Is Nothing Then threadName = "<ThreadPool thread>"

      outputBlock.Dispatcher.BeginInvoke(displayHelper, _
         String.Format("{0} is running. IsBackground = {1}" & vbLf, _
         threadName, Thread.CurrentThread.IsBackground))

   End Sub

   ' 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 Shared displayHelper As New Action(Of String)(AddressOf DisplayOutput)
   Private Shared Sub DisplayOutput(ByVal msg As String)
      outputBlock.Text &= msg 
   End Sub

End Class

' This example produces output similar to the following:
'
'BackgroundThread is running. IsBackground = True.
'ForegroundThread is running. IsBackground = False.
'<ThreadPool thread> is running. IsBackground = True.
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.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.