Raghaav
Raghaav

Reputation: 243

Thread Join method

class Program
    {
        static void Main(string[] args)
        {
            ParameterizedThreadStart aStart = new ParameterizedThreadStart(Addition);
            Thread aThread = new Thread(aStart);
            Data aData = new Data();
            aData.X = 10;
            aData.Y = 20;

        aThread.Start(aData);
        aThread.Join();
        Console.WriteLine("End of the program");
    }


    static void Addition(object data)
    {
        var a = data as Data;
        var b = a.X + a.Y;
        a.result = b;

        Console.WriteLine(a.result);
        Thread.Sleep(1000);
        Console.WriteLine("End of thread");

    }
}  

I have written an example to understand the Join method(); Can any body explain how it works ? and what is the difference between sleep and join()

Upvotes: 0

Views: 876

Answers (3)

Kalpana Krishna
Kalpana Krishna

Reputation: 9

It causes the currently running thread to stop execution till the time the thread it joins with stop execution.i.e joins method waits for a thread to die.

Upvotes: 0

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

  • Thread.Sleep

    Blocks the current thread for the specified number of milliseconds.

    -Thread.Join

    Blocks the calling thread until a thread terminates (you don't know for how long)

Note that the Thread.Join() method only blocks the calling thread (usually the application's main thread of execution) until your thread object completes. You can still have other threads executing in the background while waiting for your specific Thread to finish executing.

http://msdn.microsoft.com/en-us/library/system.threading.thread.join.aspx

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81724

Join waits until the thread you've called it on stops. Sleep sleeps for a given time period.

Upvotes: 0

Related Questions