Thread.Join Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Blokuje wątek wywołujący do momentu zakończenia wątku reprezentowanego przez to wystąpienie.
Przeciążenia
| Nazwa | Opis |
|---|---|
| Join() |
Blokuje wątek wywołujący, dopóki wątek reprezentowany przez to wystąpienie zakończy się, kontynuując wykonywanie standardowych operacji COM i |
| Join(Int32) |
Blokuje wątek wywołujący do momentu zakończenia wątku reprezentowanego przez to wystąpienie lub upływu określonego czasu podczas kontynuowania standardowej pompy COM i SendMessage. |
| Join(TimeSpan) |
Blokuje wątek wywołujący do momentu zakończenia wątku reprezentowanego przez to wystąpienie lub upływu określonego czasu podczas kontynuowania standardowej pompy COM i SendMessage. |
Join()
- Źródło:
- Thread.cs
- Źródło:
- Thread.cs
- Źródło:
- Thread.cs
- Źródło:
- Thread.cs
- Źródło:
- Thread.cs
Blokuje wątek wywołujący, dopóki wątek reprezentowany przez to wystąpienie zakończy się, kontynuując wykonywanie standardowych operacji COM i SendMessage pompowania.
public:
void Join();
public void Join();
member this.Join : unit -> unit
Public Sub Join ()
Wyjątki
Obiekt wywołujący próbował dołączyć do wątku Unstarted , który jest w stanie .
Wątek jest przerywany podczas oczekiwania.
Uwagi
Join to metoda synchronizacji, która blokuje wątek wywołujący (czyli wątek wywołujący metodę) do momentu zakończenia wątku, którego Join metoda jest wywoływana. Użyj tej metody, aby upewnić się, że wątek został przerwany. Obiekt wywołujący zablokuje czas nieokreślony, jeśli wątek nie zakończy się. W poniższym przykładzie Thread1 wątek wywołuje metodę Join()Thread2 , co powoduje Thread1 zablokowanie do Thread2 momentu ukończenia.
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
Jeśli wątek został już zakończony po Join wywołaniu, metoda zwraca natychmiast.
Warning
Nigdy nie należy wywoływać Join metody Thread obiektu reprezentującego bieżący wątek z bieżącego wątku. Powoduje to, że aplikacja przestaje odpowiadać, ponieważ bieżący wątek czeka na siebie przez czas nieokreślony.
Ta metoda zmienia stan wątku wywołującego tak, aby zawierał ThreadState.WaitSleepJoinelement . Nie można wywołać Join elementu w wątku ThreadState.Unstarted , który znajduje się w stanie .
Zobacz też
Dotyczy
Join(Int32)
- Źródło:
- Thread.CoreCLR.cs
- Źródło:
- Thread.cs
- Źródło:
- Thread.cs
- Źródło:
- Thread.cs
- Źródło:
- Thread.cs
Blokuje wątek wywołujący do momentu zakończenia wątku reprezentowanego przez to wystąpienie lub upływu określonego czasu podczas kontynuowania standardowej pompy COM i SendMessage.
public:
bool Join(int millisecondsTimeout);
public bool Join(int millisecondsTimeout);
member this.Join : int -> bool
Public Function Join (millisecondsTimeout As Integer) As Boolean
Parametry
- millisecondsTimeout
- Int32
Liczba milisekund oczekiwania na zakończenie wątku.
Zwraca
true jeśli wątek został zakończony; false jeśli wątek nie został zakończony po upływie czasu określonego millisecondsTimeout przez parametr .
Wyjątki
Wartość jest ujemna millisecondsTimeout i nie jest równa Infinite w milisekundach.
Wątek nie został uruchomiony.
millisecondsTimeout wartość jest mniejsza niż -1 (limit czasu.Nieskończony).
Wątek został przerwany podczas oczekiwania.
Uwagi
Join(Int32) to metoda synchronizacji, która blokuje wątek wywołujący (czyli wątek, który wywołuje metodę), dopóki w wątku, którego Join metoda jest wywoływana, została ukończona lub upłynął interwał limitu czasu. W poniższym przykładzie Thread1 wątek wywołuje metodę Join() , która powoduje Thread2 zablokowanie Thread1 elementu Thread2do momentu zakończenia lub 2 sekundy.
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
Jeśli Timeout.Infinite parametr jest określony millisecondsTimeout , ta metoda zachowuje się identycznie z Join() przeciążeniem metody, z wyjątkiem wartości zwracanej.
Jeśli wątek został już zakończony po Join wywołaniu, metoda zwraca natychmiast.
Ta metoda zmienia stan wątku wywołującego tak, aby zawierał ThreadState.WaitSleepJoinelement . Nie można wywołać Join elementu w wątku ThreadState.Unstarted , który znajduje się w stanie .
Zobacz też
Dotyczy
Join(TimeSpan)
- Źródło:
- Thread.cs
- Źródło:
- Thread.cs
- Źródło:
- Thread.cs
- Źródło:
- Thread.cs
- Źródło:
- Thread.cs
Blokuje wątek wywołujący do momentu zakończenia wątku reprezentowanego przez to wystąpienie lub upływu określonego czasu podczas kontynuowania standardowej pompy COM i SendMessage.
public:
bool Join(TimeSpan timeout);
public bool Join(TimeSpan timeout);
member this.Join : TimeSpan -> bool
Public Function Join (timeout As TimeSpan) As Boolean
Parametry
Zwraca
true jeśli wątek został przerwany; false jeśli wątek nie został zakończony po upływie czasu określonego timeout przez parametr .
Wyjątki
Wartość jest ujemna timeout i nie jest równa Infinite w milisekundach lub jest większa niż w milisekundach Int32.MaxValue .
Obiekt wywołujący próbował dołączyć do wątku Unstarted , który jest w stanie .
Przykłady
W poniższym przykładzie kodu pokazano, jak używać TimeSpan wartości z Join metodą .
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.
Uwagi
Join(TimeSpan) to metoda synchronizacji, która blokuje wątek wywołujący (czyli wątek, który wywołuje metodę), dopóki w wątku, którego Join metoda jest wywoływana, została ukończona lub upłynął interwał limitu czasu. W poniższym przykładzie Thread1 wątek wywołuje metodę Join() , która powoduje Thread2 zablokowanie Thread1 elementu Thread2do momentu zakończenia lub 2 sekundy.
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
Jeśli Timeout.Infinite określono parametr dla timeoutmetody , ta metoda zachowuje się identycznie z Join() przeciążeniem metody, z wyjątkiem wartości zwracanej.
Jeśli wątek został już zakończony po Join wywołaniu, metoda zwraca natychmiast.
Ta metoda zmienia stan bieżącego wątku tak, aby zawierał WaitSleepJoinelement . Nie można wywołać Join elementu w wątku ThreadState.Unstarted , który znajduje się w stanie .