Thread.Join Yöntem

Tanım

Bu örnek tarafından temsil edilen iş parçacığı sonlandırılana kadar çağıran iş parçacığını engeller.

Aşırı Yüklemeler

Name Description
Join()

Standart COM ve SendMessage pompalama gerçekleştirmeye devam ederken, bu örnek tarafından temsil edilen iş parçacığı sonlandırılana kadar çağıran iş parçacığını engeller.

Join(Int32)

Bu örnek tarafından temsil edilen iş parçacığı sonlandırılana veya belirtilen süre dolana kadar çağıran iş parçacığını engeller ve bu arada standart COM ve SendMessage pompalama işlemi gerçekleştirmeye devam eder.

Join(TimeSpan)

Bu örnek tarafından temsil edilen iş parçacığı sonlandırılana veya belirtilen süre dolana kadar çağıran iş parçacığını engeller ve bu arada standart COM ve SendMessage pompalama işlemi gerçekleştirmeye devam eder.

Join()

Kaynak:
Thread.cs
Kaynak:
Thread.cs
Kaynak:
Thread.cs
Kaynak:
Thread.cs
Kaynak:
Thread.cs

Standart COM ve SendMessage pompalama gerçekleştirmeye devam ederken, bu örnek tarafından temsil edilen iş parçacığı sonlandırılana kadar çağıran iş parçacığını engeller.

public:
 void Join();
public void Join();
member this.Join : unit -> unit
Public Sub Join ()

Özel durumlar

Çağıran, durumundaki bir iş parçacığını birleştirmeye Unstarted çalıştı.

İş parçacığı beklerken kesilir.

Açıklamalar

Join , yöntemi çağrılan iş parçacığı tamamlanana kadar çağıran Join iş parçacığını (yöntemini çağıran iş parçacığı) engelleyen bir eşitleme yöntemidir. Bir iş parçacığının sonlandırılmasını sağlamak için bu yöntemi kullanın. İş parçacığı sonlandırılmazsa çağıran süresiz olarak engeller. Aşağıdaki örnekte iş parçacığı, Thread1 tamamlanana kadar Join() engellemeye neden Thread2 olan yöntemini Thread1çağırırThread2.

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

İş parçacığı çağrıldığında zaten sonlandırıldıysa Join , yöntemi hemen döndürür.

Warning

Geçerli iş parçacığından Join geçerli iş parçacığını Thread temsil eden nesnesinin yöntemini hiçbir zaman çağırmamalısınız. Bu durum uygulamanızın yanıt vermemeye başlamasına neden olur çünkü geçerli iş parçacığı kendisini süresiz olarak bekler.

Bu yöntem, çağıran iş parçacığının durumunu içerecek ThreadState.WaitSleepJoinşekilde değiştirir. Durumundaki bir iş parçacığında Join çağıramazsınızThreadState.Unstarted.

Ayrıca bkz.

Şunlara uygulanır

Join(Int32)

Kaynak:
Thread.CoreCLR.cs
Kaynak:
Thread.cs
Kaynak:
Thread.cs
Kaynak:
Thread.cs
Kaynak:
Thread.cs

Bu örnek tarafından temsil edilen iş parçacığı sonlandırılana veya belirtilen süre dolana kadar çağıran iş parçacığını engeller ve bu arada standart COM ve SendMessage pompalama işlemi gerçekleştirmeye devam eder.

public:
 bool Join(int millisecondsTimeout);
public bool Join(int millisecondsTimeout);
member this.Join : int -> bool
Public Function Join (millisecondsTimeout As Integer) As Boolean

Parametreler

millisecondsTimeout
Int32

İş parçacığının sona ermesi için beklenen milisaniye sayısı.

Döndürülenler

true iş parçacığı sonlandırıldıysa; false parametresi tarafından millisecondsTimeout belirtilen süre dolduktan sonra iş parçacığı sonlandırılmadıysa.

Özel durumlar

değeri millisecondsTimeout negatiftir ve milisaniye cinsinden değerine eşit Infinite değildir.

İş parçacığı başlatılmadı.

millisecondsTimeout -1'den küçüktür (Timeout.Infinite).

İş parçacığı beklerken kesildi.

Açıklamalar

Join(Int32) yöntemi çağrılan iş parçacığı tamamlanana veya zaman aşımı aralığı geçene kadar çağıran iş parçacığını (yöntemini çağıran Join iş parçacığı) engelleyen bir eşitleme yöntemidir. Aşağıdaki örnekte iş parçacığı, Thread1 tamamlanana veya 2 saniye geçene kadar Join() engellemeye neden Thread2 olan yöntemini Thread1çağırırThread2.

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

parametresi için Timeout.Infinite belirtilirsemillisecondsTimeout, bu yöntem dönüş değeri dışında yöntem aşırı yüklemesine benzer şekilde Join() davranır.

İş parçacığı çağrıldığında zaten sonlandırıldıysa Join , yöntemi hemen döndürür.

Bu yöntem, çağıran iş parçacığının durumunu içerecek ThreadState.WaitSleepJoinşekilde değiştirir. Durumundaki bir iş parçacığında Join çağıramazsınızThreadState.Unstarted.

Ayrıca bkz.

Şunlara uygulanır

Join(TimeSpan)

Kaynak:
Thread.cs
Kaynak:
Thread.cs
Kaynak:
Thread.cs
Kaynak:
Thread.cs
Kaynak:
Thread.cs

Bu örnek tarafından temsil edilen iş parçacığı sonlandırılana veya belirtilen süre dolana kadar çağıran iş parçacığını engeller ve bu arada standart COM ve SendMessage pompalama işlemi gerçekleştirmeye devam eder.

public:
 bool Join(TimeSpan timeout);
public bool Join(TimeSpan timeout);
member this.Join : TimeSpan -> bool
Public Function Join (timeout As TimeSpan) As Boolean

Parametreler

timeout
TimeSpan

İş TimeSpan parçacığının sona ermesini beklemek için gereken süreye ayarlanmış.

Döndürülenler

true iş parçacığı sonlandırıldıysa; false parametresi tarafından timeout belirtilen süre dolduktan sonra iş parçacığı sonlandırılmadıysa.

Özel durumlar

değeri timeout negatiftir ve milisaniye cinsinden değerine eşit Infinite değildir veya Int32.MaxValue milisaniyeden büyüktür.

Çağıran, durumundaki bir iş parçacığını birleştirmeye Unstarted çalıştı.

Örnekler

Aşağıdaki kod örneği, yöntemiyle bir TimeSpan değerin Join nasıl kullanılacağını gösterir.

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.

Açıklamalar

Join(TimeSpan) yöntemi çağrılan iş parçacığı tamamlanana veya zaman aşımı aralığı geçene kadar çağıran iş parçacığını (yöntemini çağıran Join iş parçacığı) engelleyen bir eşitleme yöntemidir. Aşağıdaki örnekte iş parçacığı, Thread1 tamamlanana veya 2 saniye geçene kadar Join() engellemeye neden Thread2 olan yöntemini Thread1çağırırThread2.

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

için Timeout.Infinitebelirtilirsetimeout, bu yöntem dönüş değeri dışında yöntem aşırı yüklemesine benzer Join() şekilde davranır.

İş parçacığı çağrıldığında zaten sonlandırıldıysa Join , yöntemi hemen döndürür.

Bu yöntem geçerli iş parçacığının durumunu içerecek WaitSleepJoinşekilde değiştirir. Durumundaki bir iş parçacığında Join çağıramazsınızThreadState.Unstarted.

Ayrıca bkz.

Şunlara uygulanır