http://msdn.microsoft.com/zh-cn/library/23f7b1ct.aspxjoin 为 连接/加入 的意思。
MSDN 对Thread.Join() 的解释为:
在继续执行标准的 COM 和 SendMessage 消息泵处理期间,阻塞调用线程,直到某个线程终止或经过了指定时间为止。
using System;
using System.Threading;class Test
{
static TimeSpan waitTime = new TimeSpan(0, 0, 1); public static void Main()
{
Thread newThread = new Thread(new ThreadStart(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);
}
}
我对Thread.Join() 这个方法实在不好理解,以上面的代码为例,阻塞调用线程是哪个线程?直到某个线程终止 这个线程 又是哪个线程?代码中只有一个线程 newThread 啊?!
Console.WriteLine("New thread terminated.");
{
Console.WriteLine("New thread terminated.");
}
else
{
Console.WriteLine("Join timed out.");
}
如何解释?
是不是这样理解:
线程 newThread开始 即newThread.Start()后,在指定的时间段 waitTime + waitTime内,如果 线程newThread终止,则执行 Console.WriteLine("New thread terminated.");反之?
Blocks the calling thread until a thread terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.阻塞的是调用Join()这个方法的线程,等待的是Thread对象所附属的线程t.Join();的意思是,阻塞当前线程,直到线程t结束
唉,我头有点晕了。
我自己UP一下,盼“星星”回复,以确认 一下。
myThread.Abort();
myThread.Join(0);