Thread.Join Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Bloque le thread appelant jusqu’à ce que le thread représenté par cette instance se termine.
Surcharges
| Nom | Description |
|---|---|
| Join() |
Bloque le thread appelant jusqu’à ce que le thread représenté par cette instance se termine, tout en continuant à effectuer des opérations COM et |
| Join(Int32) |
Bloque le thread appelant jusqu’à ce que le thread représenté par cette instance se termine ou que le temps spécifié s’écoule, tout en continuant à effectuer une pompe COM et SendMessage standard. |
| Join(TimeSpan) |
Bloque le thread appelant jusqu’à ce que le thread représenté par cette instance se termine ou que le temps spécifié s’écoule, tout en continuant à effectuer une pompe COM et SendMessage standard. |
Join()
Bloque le thread appelant jusqu’à ce que le thread représenté par cette instance se termine, tout en continuant à effectuer des opérations COM et SendMessage de pompe standard.
public:
void Join();
public void Join();
member this.Join : unit -> unit
Public Sub Join ()
Exceptions
L’appelant a tenté de joindre un thread dans l’état Unstarted .
Le thread est interrompu en attendant.
Remarques
Join est une méthode de synchronisation qui bloque le thread appelant (autrement dit, le thread qui appelle la méthode) jusqu’à ce que le thread dont Join la méthode soit appelée est terminée. Utilisez cette méthode pour vous assurer qu’un thread a été arrêté. L’appelant se bloque indéfiniment si le thread ne se termine pas. Dans l’exemple suivant, le Thread1 thread appelle la méthode de Thread2, ce qui provoque Thread1 le Join() blocage jusqu’à ce qu’elle Thread2 soit terminée.
using System;
using System.Threading;
public class Example
{
static Thread thread1, thread2;
public static void Main()
{
thread1 = new Thread(ThreadProc);
thread1.Name = "Thread1";
thread1.Start();
thread2 = new Thread(ThreadProc);
thread2.Name = "Thread2";
thread2.Start();
}
private static void ThreadProc()
{
Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
if (Thread.CurrentThread.Name == "Thread1" &&
thread2.ThreadState != ThreadState.Unstarted)
thread2.Join();
Thread.Sleep(4000);
Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
Console.WriteLine("Thread1: {0}", thread1.ThreadState);
Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
}
}
// The example displays output like the following:
// Current thread: Thread1
//
// Current thread: Thread2
//
// Current thread: Thread2
// Thread1: WaitSleepJoin
// Thread2: Running
//
//
// Current thread: Thread1
// Thread1: Running
// Thread2: Stopped
open System.Threading
let mutable thread1, thread2 =
Unchecked.defaultof<Thread>, Unchecked.defaultof<Thread>
let threadProc () =
printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
if
Thread.CurrentThread.Name = "Thread1"
&& thread2.ThreadState <> ThreadState.Unstarted
then
thread2.Join()
Thread.Sleep 4000
printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
printfn $"Thread1: {thread1.ThreadState}"
printfn $"Thread2: {thread2.ThreadState}\n"
thread1 <- Thread threadProc
thread1.Name <- "Thread1"
thread1.Start()
thread2 <- Thread threadProc
thread2.Name <- "Thread2"
thread2.Start()
// The example displays output like the following:
// Current thread: Thread1
//
// Current thread: Thread2
//
// Current thread: Thread2
// Thread1: WaitSleepJoin
// Thread2: Running
//
//
// Current thread: Thread1
// Thread1: Running
// Thread2: Stopped
Imports System.Threading
Module Example
Dim thread1, thread2 As Thread
Public Sub Main()
thread1 = new Thread(AddressOf ThreadProc)
thread1.Name = "Thread1"
thread1.Start()
thread2 = New Thread(AddressOf ThreadProc)
thread2.Name = "Thread2"
thread2.Start()
End Sub
Private Sub ThreadProc()
Console.WriteLine()
Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
If (Thread.CurrentThread.Name = "Thread1" And
thread2.ThreadState <> ThreadState.Unstarted)
thread2.Join()
End If
Thread.Sleep(4000)
Console.WriteLine()
Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
Console.WriteLine("Thread1: {0}", thread1.ThreadState)
Console.WriteLine("Thread2: {0}", thread2.ThreadState)
Console.WriteLine()
End Sub
End Module
' The example displays output like the following :
' Current thread: Thread1
'
' Current thread: Thread2
'
' Current thread: Thread2
' Thread1: WaitSleepJoin
' Thread2: Running
'
'
' Current thread: Thread1
' Thread1: Running
' Thread2: Stopped
Si le thread s’est déjà arrêté lorsqu’il Join est appelé, la méthode retourne immédiatement.
Warning
Vous ne devez jamais appeler la Join méthode de l’objet Thread qui représente le thread actuel à partir du thread actuel. Cela entraîne l’absence de réponse de votre application, car le thread actuel attend sur lui-même indéfiniment,
Cette méthode modifie l’état du thread appelant à inclure ThreadState.WaitSleepJoin. Vous ne pouvez pas appeler Join sur un thread qui est dans l’état ThreadState.Unstarted .
Voir aussi
S’applique à
Join(Int32)
Bloque le thread appelant jusqu’à ce que le thread représenté par cette instance se termine ou que le temps spécifié s’écoule, tout en continuant à effectuer une pompe COM et SendMessage standard.
public:
bool Join(int millisecondsTimeout);
public bool Join(int millisecondsTimeout);
member this.Join : int -> bool
Public Function Join (millisecondsTimeout As Integer) As Boolean
Paramètres
- millisecondsTimeout
- Int32
Nombre de millisecondes à attendre que le thread se termine.
Retours
true si le thread s’est arrêté ; false si le thread n’a pas terminé après la durée spécifiée par le millisecondsTimeout paramètre.
Exceptions
La valeur est millisecondsTimeout négative et n’est pas égale à Infinite en millisecondes.
Le thread n’a pas été démarré.
millisecondsTimeout est inférieur à -1 (Timeout.Infinite).
Le thread a été interrompu en attendant.
Remarques
Join(Int32) est une méthode de synchronisation qui bloque le thread appelant (autrement dit, le thread qui appelle la méthode) jusqu’à ce que le thread dont Join la méthode est appelée soit terminée, soit que l’intervalle de délai d’attente s’est écoulé. Dans l’exemple suivant, le Thread1 thread appelle la Join() méthode de Thread2, ce qui provoque Thread1 le blocage jusqu’à Thread2 ce que l’exécution soit terminée ou 2 secondes s’est écoulée.
using System;
using System.Threading;
public class Example
{
static Thread thread1, thread2;
public static void Main()
{
thread1 = new Thread(ThreadProc);
thread1.Name = "Thread1";
thread1.Start();
thread2 = new Thread(ThreadProc);
thread2.Name = "Thread2";
thread2.Start();
}
private static void ThreadProc()
{
Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
if (Thread.CurrentThread.Name == "Thread1" &&
thread2.ThreadState != ThreadState.Unstarted)
if (thread2.Join(2000))
Console.WriteLine("Thread2 has termminated.");
else
Console.WriteLine("The timeout has elapsed and Thread1 will resume.");
Thread.Sleep(4000);
Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
Console.WriteLine("Thread1: {0}", thread1.ThreadState);
Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
}
}
// The example displays the following output:
// Current thread: Thread1
//
// Current thread: Thread2
// The timeout has elapsed and Thread1 will resume.
//
// Current thread: Thread2
// Thread1: WaitSleepJoin
// Thread2: Running
//
//
// Current thread: Thread1
// Thread1: Running
// Thread2: Stopped
open System.Threading
let mutable thread1, thread2 =
Unchecked.defaultof<Thread>, Unchecked.defaultof<Thread>
let threadProc () =
printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
if
Thread.CurrentThread.Name = "Thread1"
&& thread2.ThreadState <> ThreadState.Unstarted
then
if thread2.Join 2000 then
printfn "Thread2 has termminated."
else
printfn "The timeout has elapsed and Thread1 will resume."
Thread.Sleep 4000
printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
printfn $"Thread1: {thread1.ThreadState}"
printfn $"Thread2: {thread2.ThreadState}\n"
thread1 <- Thread threadProc
thread1.Name <- "Thread1"
thread1.Start()
thread2 <- Thread threadProc
thread2.Name <- "Thread2"
thread2.Start()
// The example displays the following output:
// Current thread: Thread1
//
// Current thread: Thread2
// The timeout has elapsed and Thread1 will resume.
//
// Current thread: Thread2
// Thread1: WaitSleepJoin
// Thread2: Running
//
//
// Current thread: Thread1
// Thread1: Running
// Thread2: Stopped
Imports System.Threading
Module Example
Dim thread1, thread2 As Thread
Public Sub Main()
thread1 = new Thread(AddressOf ThreadProc)
thread1.Name = "Thread1"
thread1.Start()
thread2 = New Thread(AddressOf ThreadProc)
thread2.Name = "Thread2"
thread2.Start()
End Sub
Private Sub ThreadProc()
Console.WriteLine()
Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
If (Thread.CurrentThread.Name = "Thread1" And
thread2.ThreadState <> ThreadState.Unstarted)
If thread2.Join(TimeSpan.FromSeconds(2))
Console.WriteLine("Thread2 has termminated.")
Else
Console.WriteLine("The timeout has elapsed and Thread1 will resume.")
End If
End If
Thread.Sleep(4000)
Console.WriteLine()
Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
Console.WriteLine("Thread1: {0}", thread1.ThreadState)
Console.WriteLine("Thread2: {0}", thread2.ThreadState)
Console.WriteLine()
End Sub
End Module
' The example displays the following output:
' Current thread: Thread1
'
' Current thread: Thread2
'
' Current thread: Thread2
' Thread1: WaitSleepJoin
' Thread2: Running
'
'
' Current thread: Thread1
' Thread1: Running
' Thread2: Stopped
Si Timeout.Infinite elle est spécifiée pour le millisecondsTimeout paramètre, cette méthode se comporte de façon identique à la Join() surcharge de méthode, à l’exception de la valeur de retour.
Si le thread s’est déjà arrêté lorsqu’il Join est appelé, la méthode retourne immédiatement.
Cette méthode modifie l’état du thread appelant à inclure ThreadState.WaitSleepJoin. Vous ne pouvez pas appeler Join sur un thread qui est dans l’état ThreadState.Unstarted .
Voir aussi
S’applique à
Join(TimeSpan)
Bloque le thread appelant jusqu’à ce que le thread représenté par cette instance se termine ou que le temps spécifié s’écoule, tout en continuant à effectuer une pompe COM et SendMessage standard.
public:
bool Join(TimeSpan timeout);
public bool Join(TimeSpan timeout);
member this.Join : TimeSpan -> bool
Public Function Join (timeout As TimeSpan) As Boolean
Paramètres
Retours
true si le thread s’est arrêté ; false si le thread n’a pas terminé après la durée spécifiée par le timeout paramètre.
Exceptions
La valeur est timeout négative et n’est pas égale à en millisecondes, ou est supérieure à InfiniteInt32.MaxValue millisecondes.
L’appelant a tenté de joindre un thread dans l’état Unstarted .
Exemples
L’exemple de code suivant montre comment utiliser une TimeSpan valeur avec la Join méthode.
using System;
using System.Threading;
class Test
{
static TimeSpan waitTime = new TimeSpan(0, 0, 1);
public static void Main()
{
Thread newThread = new Thread(Work);
newThread.Start();
if(newThread.Join(waitTime + waitTime)) {
Console.WriteLine("New thread terminated.");
}
else {
Console.WriteLine("Join timed out.");
}
}
static void Work()
{
Thread.Sleep(waitTime);
}
}
// The example displays the following output:
// New thread terminated.
open System
open System.Threading
let waitTime = TimeSpan(0, 0, 1)
let work () =
Thread.Sleep waitTime
let newThread = Thread work
newThread.Start()
if waitTime + waitTime |> newThread.Join then
printfn "New thread terminated."
else
printfn "Join timed out."
// The example displays the following output:
// New thread terminated.
Imports System.Threading
Public Module Test
Dim waitTime As New TimeSpan(0, 0, 1)
Public Sub Main()
Dim newThread As New Thread(AddressOf Work)
newThread.Start()
If newThread.Join(waitTime + waitTime) Then
Console.WriteLine("New thread terminated.")
Else
Console.WriteLine("Join timed out.")
End If
End Sub
Private Sub Work()
Thread.Sleep(waitTime)
End Sub
End Module
' The example displays the following output:
' New thread terminated.
Remarques
Join(TimeSpan) est une méthode de synchronisation qui bloque le thread appelant (autrement dit, le thread qui appelle la méthode) jusqu’à ce que le thread dont Join la méthode est appelée soit terminée, soit que l’intervalle de délai d’attente s’est écoulé. Dans l’exemple suivant, le Thread1 thread appelle la Join() méthode de Thread2, ce qui provoque Thread1 le blocage jusqu’à Thread2 ce que l’exécution soit terminée ou 2 secondes s’est écoulée.
using System;
using System.Threading;
public class Example
{
static Thread thread1, thread2;
public static void Main()
{
thread1 = new Thread(ThreadProc);
thread1.Name = "Thread1";
thread1.Start();
thread2 = new Thread(ThreadProc);
thread2.Name = "Thread2";
thread2.Start();
}
private static void ThreadProc()
{
Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
if (Thread.CurrentThread.Name == "Thread1" &&
thread2.ThreadState != ThreadState.Unstarted)
if (thread2.Join(TimeSpan.FromSeconds(2)))
Console.WriteLine("Thread2 has termminated.");
else
Console.WriteLine("The timeout has elapsed and Thread1 will resume.");
Thread.Sleep(4000);
Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
Console.WriteLine("Thread1: {0}", thread1.ThreadState);
Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
}
}
// The example displays the following output:
// Current thread: Thread1
//
// Current thread: Thread2
// The timeout has elapsed and Thread1 will resume.
//
// Current thread: Thread2
// Thread1: WaitSleepJoin
// Thread2: Running
//
//
// Current thread: Thread1
// Thread1: Running
// Thread2: Stopped
open System
open System.Threading
let mutable thread1, thread2 =
Unchecked.defaultof<Thread>, Unchecked.defaultof<Thread>
let threadProc () =
printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
if
Thread.CurrentThread.Name = "Thread1"
&& thread2.ThreadState <> ThreadState.Unstarted
then
if TimeSpan.FromSeconds 2 |> thread2.Join then
printfn "Thread2 has termminated."
else
printfn "The timeout has elapsed and Thread1 will resume."
Thread.Sleep 4000
printfn $"\nCurrent thread: {Thread.CurrentThread.Name}"
printfn $"Thread1: {thread1.ThreadState}"
printfn $"Thread2: {thread2.ThreadState}\n"
thread1 <- Thread threadProc
thread1.Name <- "Thread1"
thread1.Start()
thread2 <- Thread threadProc
thread2.Name <- "Thread2"
thread2.Start()
// The example displays the following output:
// Current thread: Thread1
//
// Current thread: Thread2
// The timeout has elapsed and Thread1 will resume.
//
// Current thread: Thread2
// Thread1: WaitSleepJoin
// Thread2: Running
//
//
// Current thread: Thread1
// Thread1: Running
// Thread2: Stopped
Imports System.Threading
Module Example
Dim thread1, thread2 As Thread
Public Sub Main()
thread1 = new Thread(AddressOf ThreadProc)
thread1.Name = "Thread1"
thread1.Start()
thread2 = New Thread(AddressOf ThreadProc)
thread2.Name = "Thread2"
thread2.Start()
End Sub
Private Sub ThreadProc()
Console.WriteLine()
Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
If (Thread.CurrentThread.Name = "Thread1" And
thread2.ThreadState <> ThreadState.Unstarted)
If thread2.Join(2000)
Console.WriteLine("Thread2 has termminated.")
Else
Console.WriteLine("The timeout has elapsed and Thread1 will resume.")
End If
End If
Thread.Sleep(4000)
Console.WriteLine()
Console.WriteLine("Current thread: {0}", Thread.CurrentThread.Name)
Console.WriteLine("Thread1: {0}", thread1.ThreadState)
Console.WriteLine("Thread2: {0}", thread2.ThreadState)
Console.WriteLine()
End Sub
End Module
' The example displays the following output:
' Current thread: Thread1
'
' Current thread: Thread2
'
' Current thread: Thread2
' Thread1: WaitSleepJoin
' Thread2: Running
'
'
' Current thread: Thread1
' Thread1: Running
' Thread2: Stopped
Si Timeout.Infinite elle est spécifiée pour timeout, cette méthode se comporte de façon identique à la Join() surcharge de méthode, à l’exception de la valeur de retour.
Si le thread s’est déjà arrêté lorsqu’il Join est appelé, la méthode retourne immédiatement.
Cette méthode modifie l’état du thread actuel à inclure WaitSleepJoin. Vous ne pouvez pas appeler Join sur un thread qui est dans l’état ThreadState.Unstarted .