Partager via


Thread.IsBackground Propriété

Définition

Obtient ou définit une valeur indiquant si un thread est ou non un thread d’arrière-plan.

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.

Exceptions

Le thread est mort.

Exemples

L’exemple suivant contraste le comportement des threads de premier plan et d’arrière-plan. Il crée un thread de premier plan et un thread d’arrière-plan. Le thread de premier plan conserve le processus en cours d’exécution jusqu’à ce qu’il termine sa for boucle et se termine. Toutefois, comme le montre la sortie de l’exemple, car le thread de premier plan a terminé l’exécution, le processus est arrêté avant l’exécution du thread d’arrière-plan.

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.

Remarques

Un thread est un thread d’arrière-plan ou un thread de premier plan. 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 la fin d’un processus. Une fois que tous les threads de premier plan appartenant à un processus se sont arrêtés, le Common Language Runtime met fin au processus. Les threads d’arrière-plan restants sont arrêtés et ne sont pas terminés.

Par défaut, les threads suivants s’exécutent au premier plan (autrement dit, leur IsBackground propriété retourne false) :

  • Thread principal (ou thread d’application principal).

  • Tous les threads créés en appelant un Thread constructeur de classe.

Par défaut, les threads suivants s’exécutent en arrière-plan (autrement dit, leur IsBackground propriété retourne true) :

  • Threads de pool de threads, qui sont un pool de threads de travail gérés par le runtime. Vous pouvez configurer le pool de threads et planifier le travail dans les threads du pool en utilisant la classe ThreadPool.

    Note

    Les opérations asynchrones basées sur des tâches s’exécutent automatiquement sur les threads du pool de threads.

  • Tous les threads qui entrent dans l’environnement d’exécution managé à partir du code non managé.

S’applique à

Voir aussi