Thread.IsBackground Property
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Gets or sets a value that indicates whether a thread is a background thread.
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System.Booleantrue if this thread is or is to become a background thread; otherwise, false.
| Exception | Condition |
|---|---|
| ThreadStateException | Execution of the thread has terminated. |
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.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
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. */
Note: