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开发者_如何学C = 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()
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
Join
waits until the thread you've called it on stops. Sleep
sleeps for a given time period.
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.
精彩评论