Thread.IsBackground Property

July 28, 2014

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

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

'Declaration
Public Property IsBackground As Boolean

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.


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.


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft