Threads Joining : Thread Join « Thread « C# / CSharp Tutorial






using System;
using System.Threading;

class Util
{
    public void DoSleep()
    {
        Console.WriteLine("Sleepping {0} seconds", 5);
        Thread.Sleep(5 * 1000);
    }
    
    public static Thread Sleep()
    {
        Util ts = new Util();
        Thread thread = new Thread(new ThreadStart(ts.DoSleep));
        thread.Start();
        return(thread);
    }
}

class MainClass
{
    public static void Main()
    {
        Thread thread = Util.Sleep();
        
        Console.WriteLine("Waiting for thread to join");
        thread.Join();
        Console.WriteLine("Thread Joined");
    }
}
Sleepping 5 seconds
Waiting for thread to join
Thread Joined








20.5.Thread Join
20.5.1.Use Join() to wait for threads to end
20.5.2.Threads Joining
20.5.3.Thread joins
20.5.4.Block until a thread finishes, or timeout after 2 seconds