Thread.Join Metoda

Definicja

Blokuje wątek wywołujący do momentu zakończenia wątku reprezentowanego przez to wystąpienie.

Przeciążenia

Join()

Blokuje wątek wywołujący do momentu zakończenia wątku reprezentowanego przez to wystąpienie, przy jednoczesnym kontynuowaniu standardowych operacji COM i SendMessage pompowania.

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, przy jednoczesnym kontynuowaniu standardowej pompowania 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, przy jednoczesnym kontynuowaniu standardowej pompowania COM i SendMessage.

Join()

Ź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, przy jednoczesnym kontynuowaniu 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 wywołujący wątek (czyli wątek, który wywołuje 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ł zakończony. Obiekt wywołujący będzie blokował bezterminowo, jeśli wątek nie zostanie zakończony. W poniższym przykładzie Thread1 wątek wywołuje metodę Thread2Join() , która 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.

Ostrzeżenie

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 na czas nieokreślony.

Ta metoda zmienia stan wątku wywołującego w celu uwzględnienia ThreadState.WaitSleepJoinelementu . Nie można wywołać Join elementu w wątku ThreadState.Unstarted , który jest w stanie .

Zobacz też

Dotyczy

Join(Int32)

Ź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, przy jednoczesnym kontynuowaniu standardowej pompowania 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 wywołujący wątek (czyli wątek wywołujący metodę) do momentu zakończenia wątku, którego Join metoda jest wywoływana, lub upłynął interwał limitu czasu. W poniższym przykładzie Thread1 wątek wywołuje metodę Thread2Join() , która powoduje Thread1 zablokowanie Thread2 obiektu do 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 określono parametr dla parametru 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 w celu uwzględnienia ThreadState.WaitSleepJoinelementu . Nie można wywołać Join elementu w wątku ThreadState.Unstarted , który jest w stanie .

Zobacz też

Dotyczy

Join(TimeSpan)

Ź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, przy jednoczesnym kontynuowaniu standardowej pompowania 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

timeout
TimeSpan

Zestaw TimeSpan czasu oczekiwania na zakończenie wątku.

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ść timeout jest ujemna i nie jest równa Infinite w milisekundach lub jest większa niż Int32.MaxValue milisekund.

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 namespace System;
using namespace System::Threading;

static TimeSpan waitTime = TimeSpan(0,0,1);

ref class Test
{
public:
   static void Work()
   {
      Thread::Sleep( waitTime );
   }

};

int main()
{
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( Test::Work ) );
   newThread->Start();
   if ( newThread->Join( waitTime + waitTime ) )
   {
      Console::WriteLine( "New thread terminated." );
   }
   else
   {
      Console::WriteLine( "Join timed out." );
   }
}
// The example displays the following output:
//        New thread terminated.
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 wywołujący wątek (czyli wątek wywołujący metodę) do momentu zakończenia wątku, którego Join metoda jest wywoływana, lub upłynął interwał limitu czasu. W poniższym przykładzie Thread1 wątek wywołuje metodę Thread2Join() , która powoduje Thread1 zablokowanie Thread2 obiektu do 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 jest w stanie .

Zobacz też

Dotyczy