Thread.IsBackground Propriété

Définition

Obtient ou définit une valeur indiquant si le thread est un thread d'arrière-plan ou non.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

Valeur de propriété

true si ce thread est ou doit devenir un thread d'arrière-plan ; sinon, false.true if this thread is or is to become a background thread; otherwise, false.

Exceptions

Le thread est inactif.The thread is dead.

Exemples

L’exemple suivant compare le comportement des threads de premier plan et d’arrière-plan.The following example contrasts the behavior of foreground and background threads. Il crée un thread de premier plan et un thread d’arrière-plan.It creates a foreground thread and a background thread. Le thread de premier plan maintient le processus en cours d’exécution jusqu’à ce qu’il termine sa boucle for et se termine.The foreground thread keeps the process running until completes its for loop and terminates. Toutefois, comme le montre la sortie de l’exemple, étant donné que le thread de premier plan a terminé son exécution, le processus est terminé avant la fin de l’exécution du thread d’arrière-plan.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.

Remarques

Un thread est soit un thread d’arrière-plan, soit un thread de premier plan.A thread is either a background thread or a foreground thread. Les threads d’arrière-plan sont identiques aux threads de premier plan, sauf que les threads d’arrière-plan n’empêchent pas un processus de se terminerBackground threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Une fois que tous les threads de premier plan appartenant à un processus sont terminés, le common language runtime met fin au processus.Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Les threads d’arrière-plan restants sont arrêtés et ne se terminent pas.Any remaining background threads are stopped and do not complete.

Par défaut, les threads suivants s’exécutent au premier plan (autrement dit, leur propriété IsBackground retourne false) :By default, the following threads execute in the foreground (that is, their IsBackground property returns false):

  • Thread principal (ou thread d’application principal).The primary thread (or main application thread).

  • Tous les threads créés en appelant un constructeur de classe Thread.All threads created by calling a Thread class constructor.

Par défaut, les threads suivants s’exécutent en arrière-plan (autrement dit, leur propriété IsBackground retourne true) :By default, the following threads execute in the background (that is, their IsBackground property returns true):

  • Threads de pool de threads, qui sont un pool de threads de travail gérés par le Runtime.Thread pool threads, which are a pool of worker threads maintained by the runtime. Vous pouvez configurer le pool de threads et planifier le travail sur les threads de pool de threads à l’aide de la classe ThreadPool.You can configure the thread pool and schedule work on thread pool threads by using the ThreadPool class.

    Notes

    Les opérations asynchrones basées sur des tâches s’exécutent automatiquement sur des threads de pool de threads.Task-based asynchronous operations automatically execute on thread pool threads.

  • Tous les threads qui entrent dans l’environnement d’exécution managé à partir du code non managé.All threads that enter the managed execution environment from unmanaged code.

S’applique à

Voir aussi