Thread.Join Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee blokkeert u de aanroepende thread totdat de thread die door dit exemplaar wordt vertegenwoordigd, wordt beëindigd.
Overloads
| Name | Description |
|---|---|
| Join() |
Hiermee blokkeert u de aanroepende thread totdat de thread die door dit exemplaar wordt vertegenwoordigd, wordt beëindigd, terwijl u de standaard COM en |
| Join(Int32) |
Hiermee blokkeert u de aanroepende thread totdat de thread die door dit exemplaar wordt vertegenwoordigd, wordt beëindigd of de opgegeven tijd is verstreken, terwijl u de standaard-COM- en SendMessage-pomp blijft uitvoeren. |
| Join(TimeSpan) |
Hiermee blokkeert u de aanroepende thread totdat de thread die door dit exemplaar wordt vertegenwoordigd, wordt beëindigd of de opgegeven tijd is verstreken, terwijl u de standaard-COM- en SendMessage-pomp blijft uitvoeren. |
Join()
Hiermee blokkeert u de aanroepende thread totdat de thread die door dit exemplaar wordt vertegenwoordigd, wordt beëindigd, terwijl u de standaard COM en SendMessage pomp blijft uitvoeren.
public:
void Join();
public void Join();
member this.Join : unit -> unit
Public Sub Join ()
Uitzonderingen
De beller heeft geprobeerd lid te worden van een thread die de Unstarted status heeft.
De thread wordt onderbroken tijdens het wachten.
Opmerkingen
Join is een synchronisatiemethode waarmee de aanroepende thread (dat wil gezegd de thread die de methode aanroept) wordt geblokkeerd totdat de thread waarvan Join de methode wordt aangeroepen, is voltooid. Gebruik deze methode om ervoor te zorgen dat een thread is beëindigd. De aanroeper blokkeert voor onbepaalde tijd als de thread niet wordt beëindigd. In het volgende voorbeeld roept de Thread1 thread de Join() methode aan, Thread2waardoor Thread1 wordt geblokkeerd totdat Thread2 het is voltooid.
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
Als de thread al is beëindigd wanneer Join deze wordt aangeroepen, retourneert de methode onmiddellijk.
Warning
U moet nooit de Join methode aanroepen van het Thread object dat de huidige thread van de huidige thread vertegenwoordigt. Dit zorgt ervoor dat uw app niet meer reageert omdat de huidige thread voor onbepaalde tijd op zichzelf wacht,
Met deze methode wordt de status van de aanroepende thread gewijzigd zodat deze wordt opgenomen ThreadState.WaitSleepJoin. U kunt geen thread aanroepen Join die de ThreadState.Unstarted status heeft.
Zie ook
Van toepassing op
Join(Int32)
Hiermee blokkeert u de aanroepende thread totdat de thread die door dit exemplaar wordt vertegenwoordigd, wordt beëindigd of de opgegeven tijd is verstreken, terwijl u de standaard-COM- en SendMessage-pomp blijft uitvoeren.
public:
bool Join(int millisecondsTimeout);
public bool Join(int millisecondsTimeout);
member this.Join : int -> bool
Public Function Join (millisecondsTimeout As Integer) As Boolean
Parameters
- millisecondsTimeout
- Int32
Het aantal milliseconden dat moet worden gewacht totdat de thread is beëindigd.
Retouren
true als de thread is beëindigd; false als de thread niet is beëindigd nadat de hoeveelheid tijd die is opgegeven door de millisecondsTimeout parameter is verstreken.
Uitzonderingen
De waarde van millisecondsTimeout is negatief en is niet gelijk aan Infinite in milliseconden.
De thread is niet gestart.
millisecondsTimeout is kleiner dan -1 (Timeout.Infinite).
De thread is onderbroken tijdens het wachten.
Opmerkingen
Join(Int32) is een synchronisatiemethode waarmee de aanroepende thread (de thread die de methode aanroept) wordt geblokkeerd totdat de thread waarvan Join de methode wordt aangeroepen, is voltooid of het time-outinterval is verstreken. In het volgende voorbeeld roept de Thread1 thread de Join() methode aan, Thread2waardoor Thread1 wordt geblokkeerd totdat Thread2 het voltooid is of 2 seconden zijn verstreken.
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
Als Timeout.Infinite deze methode is opgegeven voor de millisecondsTimeout parameter, gedraagt deze methode zich identiek aan de overbelasting van de Join() methode, met uitzondering van de retourwaarde.
Als de thread al is beëindigd wanneer Join deze wordt aangeroepen, retourneert de methode onmiddellijk.
Met deze methode wordt de status van de aanroepende thread gewijzigd zodat deze wordt opgenomen ThreadState.WaitSleepJoin. U kunt geen thread aanroepen Join die de ThreadState.Unstarted status heeft.
Zie ook
Van toepassing op
Join(TimeSpan)
Hiermee blokkeert u de aanroepende thread totdat de thread die door dit exemplaar wordt vertegenwoordigd, wordt beëindigd of de opgegeven tijd is verstreken, terwijl u de standaard-COM- en SendMessage-pomp blijft uitvoeren.
public:
bool Join(TimeSpan timeout);
public bool Join(TimeSpan timeout);
member this.Join : TimeSpan -> bool
Public Function Join (timeout As TimeSpan) As Boolean
Parameters
- timeout
- TimeSpan
Een TimeSpan waarde die is ingesteld op de hoeveelheid tijd die moet worden gewacht totdat de thread is beëindigd.
Retouren
true indien de thread is beëindigd; false als de thread niet is beëindigd nadat de hoeveelheid tijd die is opgegeven door de timeout parameter is verstreken.
Uitzonderingen
De waarde is timeout negatief en is niet gelijk aan Infinite in milliseconden of is groter dan Int32.MaxValue milliseconden.
De beller heeft geprobeerd lid te worden van een thread die de Unstarted status heeft.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u een TimeSpan waarde gebruikt met de Join methode.
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.
Opmerkingen
Join(TimeSpan) is een synchronisatiemethode waarmee de aanroepende thread (de thread die de methode aanroept) wordt geblokkeerd totdat de thread waarvan Join de methode wordt aangeroepen, is voltooid of het time-outinterval is verstreken. In het volgende voorbeeld roept de Thread1 thread de Join() methode aan, Thread2waardoor Thread1 wordt geblokkeerd totdat Thread2 het voltooid is of 2 seconden zijn verstreken.
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
Als Timeout.Infinite hiervoor is opgegeven timeout, gedraagt deze methode zich identiek aan de overbelasting van de Join() methode, met uitzondering van de retourwaarde.
Als de thread al is beëindigd wanneer Join deze wordt aangeroepen, retourneert de methode onmiddellijk.
Met deze methode wordt de status van de huidige thread gewijzigd zodat deze wordt opgenomen WaitSleepJoin. U kunt geen thread aanroepen Join die de ThreadState.Unstarted status heeft.