Thread.IsBackground Tulajdonság
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Lekéri vagy beállít egy értéket, amely jelzi, hogy egy szál háttérszál-e.
public:
property bool IsBackground { bool get(); void set(bool value); };
public bool IsBackground { get; set; }
member this.IsBackground : bool with get, set
Public Property IsBackground As Boolean
Tulajdonság értéke
trueha ez a szál háttérszál, vagy háttérszál lesz belőle; egyéb esetben. false
Kivételek
A szál halott.
Példák
Az alábbi példa az előtér- és háttérszálak viselkedését hasonlítja össze. Előtérszálat és háttérszálat hoz létre. Az előtérszál addig futtatja a folyamatot, amíg be nem fejeződik a ciklus, for és leáll. Ahogy azonban a példa kimenete is mutatja, mivel az előtérszál befejezte a végrehajtást, a folyamat a háttérszál végrehajtása előtt leáll.
using System;
using System.Threading;
class Example
{
static void Main()
{
BackgroundTest shortTest = new BackgroundTest(10);
Thread foregroundThread =
new Thread(new ThreadStart(shortTest.RunLoop));
BackgroundTest longTest = new BackgroundTest(50);
Thread backgroundThread =
new Thread(new ThreadStart(longTest.RunLoop));
backgroundThread.IsBackground = true;
foregroundThread.Start();
backgroundThread.Start();
}
}
class BackgroundTest
{
int maxIterations;
public BackgroundTest(int maxIterations)
{
this.maxIterations = maxIterations;
}
public void RunLoop()
{
for (int i = 0; i < maxIterations; i++) {
Console.WriteLine("{0} count: {1}",
Thread.CurrentThread.IsBackground ?
"Background Thread" : "Foreground Thread", i);
Thread.Sleep(250);
}
Console.WriteLine("{0} finished counting.",
Thread.CurrentThread.IsBackground ?
"Background Thread" : "Foreground Thread");
}
}
// The example displays output like the following:
// Foreground Thread count: 0
// Background Thread count: 0
// Background Thread count: 1
// Foreground Thread count: 1
// Foreground Thread count: 2
// Background Thread count: 2
// Foreground Thread count: 3
// Background Thread count: 3
// Background Thread count: 4
// Foreground Thread count: 4
// Foreground Thread count: 5
// Background Thread count: 5
// Foreground Thread count: 6
// Background Thread count: 6
// Background Thread count: 7
// Foreground Thread count: 7
// Background Thread count: 8
// Foreground Thread count: 8
// Foreground Thread count: 9
// Background Thread count: 9
// Background Thread count: 10
// Foreground Thread count: 10
// Background Thread count: 11
// Foreground Thread finished counting.
open System.Threading
type BackgroundTest(maxIterations) =
member _.RunLoop() =
for i = 0 to maxIterations - 1 do
printfn
$"""{if Thread.CurrentThread.IsBackground then
"Background Thread"
else
"Foreground Thread"} count: {i}"""
Thread.Sleep 250
printfn
$"""{if Thread.CurrentThread.IsBackground then
"Background Thread"
else
"Foreground Thread"} finished counting."""
let shortTest = BackgroundTest 10
let foregroundThread = Thread shortTest.RunLoop
let longTest = BackgroundTest 50
let backgroundThread = Thread longTest.RunLoop
backgroundThread.IsBackground <- true
foregroundThread.Start()
backgroundThread.Start()
// The example displays output like the following:
// Foreground Thread count: 0
// Background Thread count: 0
// Background Thread count: 1
// Foreground Thread count: 1
// Foreground Thread count: 2
// Background Thread count: 2
// Foreground Thread count: 3
// Background Thread count: 3
// Background Thread count: 4
// Foreground Thread count: 4
// Foreground Thread count: 5
// Background Thread count: 5
// Foreground Thread count: 6
// Background Thread count: 6
// Background Thread count: 7
// Foreground Thread count: 7
// Background Thread count: 8
// Foreground Thread count: 8
// Foreground Thread count: 9
// Background Thread count: 9
// Background Thread count: 10
// Foreground Thread count: 10
// Background Thread count: 11
// Foreground Thread finished counting.
Imports System.Threading
Public Module Example
Public Sub Main()
Dim shortTest As New BackgroundTest(10)
Dim foregroundThread As New Thread(AddressOf shortTest.RunLoop)
Dim longTest As New BackgroundTest(50)
Dim backgroundThread As New Thread(AddressOf longTest.RunLoop)
backgroundThread.IsBackground = True
foregroundThread.Start()
backgroundThread.Start()
End Sub
End Module
Public Class BackgroundTest
Dim maxIterations As Integer
Sub New(maximumIterations As Integer)
maxIterations = maximumIterations
End Sub
Sub RunLoop()
For i As Integer = 0 To maxIterations
Console.WriteLine("{0} count: {1}", _
If(Thread.CurrentThread.IsBackground,
"Background Thread", "Foreground Thread"), i)
Thread.Sleep(250)
Next
Console.WriteLine("{0} finished counting.",
If(Thread.CurrentThread.IsBackground,
"Background Thread", "Foreground Thread"))
End Sub
End Class
' The example displays output like the following:
' Foreground Thread count: 0
' Background Thread count: 0
' Background Thread count: 1
' Foreground Thread count: 1
' Foreground Thread count: 2
' Background Thread count: 2
' Foreground Thread count: 3
' Background Thread count: 3
' Background Thread count: 4
' Foreground Thread count: 4
' Foreground Thread count: 5
' Background Thread count: 5
' Foreground Thread count: 6
' Background Thread count: 6
' Background Thread count: 7
' Foreground Thread count: 7
' Background Thread count: 8
' Foreground Thread count: 8
' Foreground Thread count: 9
' Background Thread count: 9
' Background Thread count: 10
' Foreground Thread count: 10
' Background Thread count: 11
' Foreground Thread finished counting.
Megjegyzések
A szálak háttérszálak vagy előtérszálak. A háttérszálak megegyeznek az előtérszálakéval, azzal a kivételrel, hogy a háttérszálak nem akadályozzák a folyamat leállását. Ha egy folyamat összes előtérszála leállt, a közös nyelvi futtatókörnyezet befejezi a folyamatot. A fennmaradó háttérszálak leállnak, és nem fejeződnek be.
Alapértelmezés szerint a következő szálak futnak az előtérben (vagyis a tulajdonságuk IsBackground ad vissza false):
Az elsődleges szál (vagy fő alkalmazásszál).
Az Thread osztálykonstruktor meghívásával létrehozott szálak összessége.
Alapértelmezés szerint a következő szálak futnak a háttérben (azaz a tulajdonságuk IsBackground visszaadja true):
A szálkészlet szálai, amelyek a futtatókörnyezet által karbantartott munkaszálak készletei. Az osztály használatával konfigurálhatja a szálkészletet, és ütemezheti a ThreadPool szálkészlet-szálakon végzett munkát.
Note
A feladat-alapú aszinkron műveletek automatikusan futnak a szálkészlet szálain.
Minden szál, amely belép a felügyelt végrehajtási környezetbe a nem felügyelt kódból.