Thread.IsBackground Thread.IsBackground Thread.IsBackground Thread.IsBackground Property

Definizione

Ottiene o imposta un valore che indica se il thread viene eseguito in background.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

Valore della proprietà

true se il thread viene o verrà eseguito in background; in caso contrario, false.true if this thread is or is to become a background thread; otherwise, false.

Eccezioni

Esempi

Nell'esempio seguente viene contrastato il comportamento dei thread in primo piano e in background.The following example contrasts the behavior of foreground and background threads. Viene creato un thread in primo piano e un thread in background.It creates a foreground thread and a background thread. Il thread in primo piano mantiene il processo in esecuzione fino al completamento del ciclo for e termina.The foreground thread keeps the process running until completes its for loop and terminates. Tuttavia, come illustrato nell'output dell'esempio, perché il thread in primo piano ha terminato l'esecuzione, il processo viene terminato prima del completamento dell'esecuzione del thread in background.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.

Commenti

Un thread può essere un thread in background o un thread in primo piano.A thread is either a background thread or a foreground thread. I thread in background sono identici ai thread in primo piano, ad eccezione del fatto che i thread in background non impediscono la chiusura di un processo.Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Una volta terminati tutti i thread in primo piano che appartengono a un processo, il Common Language Runtime termina il processo.Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Tutti i thread in background rimanenti vengono arrestati e non vengono completati.Any remaining background threads are stopped and do not complete.

Per impostazione predefinita, i thread seguenti vengono eseguiti in primo piano (ovvero la proprietà IsBackground restituisce false):By default, the following threads execute in the foreground (that is, their IsBackground property returns false):

  • Thread principale (o thread dell'applicazione principale).The primary thread (or main application thread).

  • Tutti i thread creati chiamando un costruttore della classe Thread.All threads created by calling a Thread class constructor.

Per impostazione predefinita, i thread seguenti vengono eseguiti in background (ovvero la proprietà IsBackground restituisce true):By default, the following threads execute in the background (that is, their IsBackground property returns true):

  • Thread del pool di thread, ovvero un pool di thread di lavoro gestiti dal runtime.Thread pool threads, which are a pool of worker threads maintained by the runtime. È possibile configurare il pool di thread e pianificare il lavoro nei thread del pool di thread usando la classe ThreadPool.You can configure the thread pool and schedule work on thread pool threads by using the ThreadPool class.

    Nota

    Le operazioni asincrone basate su attività vengono eseguite automaticamente nei thread del pool di thread.Task-based asynchronous operations automatically execute on thread pool threads.

  • Tutti i thread che entrano nell'ambiente di esecuzione gestito da codice non gestito.All threads that enter the managed execution environment from unmanaged code.

Si applica a

Vedi anche