Saturday, February 23, 2013

Threading: 05- Waiting for thread to complete- Thread.Join

We can wait for a thread to complete using the thread’s Join method.

 

using System;
using System.Threading;

class Program {
    static void Main(string[] args) {
        Console.WriteLine("Main Started");
        var th = new Thread(Greet); // Create new thread
        th.Start(new object[] { "Bob", "Good Morning" });  // Execute Greet method in the created thread
        th.Join();
        Console.WriteLine("Main Completed");
        Console.ReadKey();
    }

    private static void Greet(object info) {
        Console.WriteLine("Hi {0}, {1}!", (info as object[])[0], (info as object[])[1]);
    }
}

 

Join method will block the calling thread until the thread completes.

No comments:

Post a Comment