Condividi tramite


Thread.IsBackground Proprietà

Definizione

Ottiene o imposta un valore che indica se il thread viene eseguito in background.

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à

Boolean

true se il thread viene o verrà eseguito in background; in caso contrario, false.

Eccezioni

Thread inattivo.

Esempio

Nell'esempio seguente viene contrastato il comportamento dei thread in primo piano e in background. Crea un thread in primo piano e un thread in background. Il thread in primo piano mantiene il processo in esecuzione finché non completa for il ciclo e termina. Tuttavia, come illustrato nell'output dell'esempio, poiché l'esecuzione del thread in primo piano è stata completata, il processo viene terminato prima del completamento dell'esecuzione del thread in background.

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 è un thread in background o un thread in primo piano. I thread in background sono identici ai thread in primo piano, ad eccezione del fatto che i thread in background non impediscono la terminazione di un processo. Dopo che tutti i thread in primo piano appartenenti a un processo sono stati terminati, Common Language Runtime termina il processo. Tutti i thread in background rimanenti vengono arrestati e non vengono completati.

Per impostazione predefinita, i thread seguenti vengono eseguiti in primo piano, ovvero la relativa IsBackground proprietà restituisce false :

  • Thread primario (o thread principale dell'applicazione).

  • Tutti i thread creati chiamando un Thread costruttore di classe.

Per impostazione predefinita, i thread seguenti vengono eseguiti in background, ovvero la relativa IsBackground proprietà restituisce true :

  • Thread del pool di thread, ovvero un pool di thread di lavoro gestiti dal runtime. È possibile configurare il pool di thread e pianificare il lavoro sui thread del pool di thread usando la ThreadPool classe .

    Nota

    Le operazioni asincrone basate su attività vengono eseguite automaticamente nei thread del pool di thread.

  • Tutti i thread che entrano nell'ambiente di esecuzione gestito da codice non gestito.

Si applica a

Vedi anche