Thread.IsBackground Właściwość

Definicja

Pobiera lub ustawia wartość wskazującą, czy wątek jest wątkiem w tle.

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

Wartość właściwości

true jeśli ten wątek jest lub ma stać się wątkiem w tle; w przeciwnym razie , false.

Wyjątki

Wątek nie żyje.

Przykłady

Poniższy przykład kontrastuje zachowanie wątków pierwszego planu i tła. Tworzy wątek pierwszego planu i wątek tła. Wątek pierwszego planu utrzymuje proces uruchomiony do momentu ukończenia for pętli i zakończenia. Jednak jak pokazuje dane wyjściowe z przykładu, ponieważ wątek pierwszego planu zakończył wykonywanie, proces zostaje zakończony przed zakończeniem wykonywania wątku w tle.

using namespace System;
using namespace System::Threading;

ref class BackgroundTest
{
private:
   int maxIterations;

public:
   BackgroundTest(int maxIterations)
   {
      this->maxIterations = maxIterations;
   }

   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");
   }
};

int main()
{
   BackgroundTest^ shortTest = gcnew BackgroundTest( 10 );
   Thread^ foregroundThread = gcnew Thread( gcnew ThreadStart( shortTest, &BackgroundTest::RunLoop ) );
   foregroundThread->Name =  "ForegroundThread";
   BackgroundTest^ longTest = gcnew BackgroundTest( 50 );
   Thread^ backgroundThread = gcnew Thread( gcnew ThreadStart( longTest, &BackgroundTest::RunLoop ) );
   backgroundThread->Name =  "BackgroundThread";
   backgroundThread->IsBackground = true;
   foregroundThread->Start();
   backgroundThread->Start();
}
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.

Uwagi

Wątek jest wątkiem tła lub wątkiem pierwszego planu. Wątki w tle są identyczne z wątkami pierwszego planu, z tą różnicą, że wątki w tle nie uniemożliwiają zakończenia procesu. Po zakończeniu wszystkich wątków pierwszego planu należących do procesu środowisko uruchomieniowe języka wspólnego kończy proces. Wszystkie pozostałe wątki w tle są zatrzymywane i nie są kompletne.

Domyślnie następujące wątki są wykonywane na pierwszym planie (czyli ich IsBackground właściwość zwraca wartość false):

  • Wątek podstawowy (lub główny wątek aplikacji).

  • Wszystkie wątki utworzone przez wywołanie konstruktora Thread klasy.

Domyślnie następujące wątki są wykonywane w tle (czyli ich IsBackground właściwość zwraca wartość true):

  • Wątki puli wątków, które są pulą wątków roboczych obsługiwanych przez środowisko uruchomieniowe. Pulę wątków można skonfigurować i zaplanować pracę w wątkach puli wątków przy użyciu ThreadPool klasy .

    Uwaga

    Operacje asynchroniczne oparte na zadaniach są wykonywane automatycznie w wątkach puli wątków.

  • Wszystkie wątki, które wchodzą w zarządzane środowisko wykonywania z niezarządzanego kodu.

Dotyczy

Zobacz też