Thread.IsBackground Vlastnost

Definice

Získá nebo nastaví hodnotu označující, zda se jedná o vlákno na pozadí.Gets or sets a value indicating whether or not a thread is a background thread.

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

Hodnota vlastnosti

true, zda je toto vlákno nebo se stane vláknem na pozadí; v opačném případě false.true if this thread is or is to become a background thread; otherwise, false.

Výjimky

Vlákno je neaktivní.The thread is dead.

Příklady

Následující příklad kontrastuje chování vláken v popředí a na pozadí.The following example contrasts the behavior of foreground and background threads. Vytvoří vlákno na popředí a vlákno na pozadí.It creates a foreground thread and a background thread. Vlákno na popředí udržuje proces spuštěný, dokud nedokončí jeho for cyklus a končí.The foreground thread keeps the process running until completes its for loop and terminates. Jak však výstup z příkladu ukazuje, protože vlákno na popředí bylo dokončeno, proces je ukončen před dokončením provádění vlákna na pozadí.However, as the output from the example shows, because the foreground thread has finished execution, the process is terminated before the background thread has completed execution.

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.
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.

Poznámky

Vlákno je buď vlákno na pozadí, nebo vlákno na popředí.A thread is either a background thread or a foreground thread. Vlákna na pozadí jsou shodná s vlákny na popředí, s tím rozdílem, že vlákna na pozadí nebrání ukončení procesu.Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Po ukončení všech vláken na popředí, která patří do procesu, ukončí modul CLR (Common Language Runtime) proces.Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Všechna zbývající vlákna na pozadí jsou zastavena a nedokončeny.Any remaining background threads are stopped and do not complete.

Ve výchozím nastavení jsou následující vlákna spouštěna v popředí (to znamená, že jejich IsBackground vlastnost vrací false):By default, the following threads execute in the foreground (that is, their IsBackground property returns false):

  • Primární vlákno (nebo hlavní vlákno aplikace).The primary thread (or main application thread).

  • Všechna vlákna vytvořená voláním konstruktoru třídy Thread.All threads created by calling a Thread class constructor.

Ve výchozím nastavení jsou následující vlákna spouštěna na pozadí (to znamená, že jejich IsBackground vlastnost vrací true):By default, the following threads execute in the background (that is, their IsBackground property returns true):

  • Vlákna fondu vláken, která jsou fondem pracovních vláken udržovaných modulem runtime.Thread pool threads, which are a pool of worker threads maintained by the runtime. Můžete nakonfigurovat fond vláken a naplánovat práci ve fondu vláken pomocí třídy ThreadPool.You can configure the thread pool and schedule work on thread pool threads by using the ThreadPool class.

    Poznámka

    Asynchronní operace založené na úlohách se automaticky spouštějí v vláknech fondu vláken.Task-based asynchronous operations automatically execute on thread pool threads.

  • Všechna vlákna, která vstupují do spravovaného prostředí pro spuštění z nespravovaného kódu.All threads that enter the managed execution environment from unmanaged code.

Platí pro

Viz také