为什么这段代码没有到达Console.WriteLine(“其他线程已经完成!”);?这段代码来自专业C# 5.0和.NET 4.5框架书,第717-718页。
private static AutoResetEvent waitHandle = new AutoResetEvent(false);
static void Main(string[] args)
{
Console.WriteLine("ID of thread in Main(): {0}", Thread.CurrentThread.ManagedThreadId);
AddParms data = new AddParms(3, 4);
Thread t = new Thread(new ParameterizedThreadStart(Add));
t.Start(data);
waitHandle.WaitOne();
Console.WriteLine("Other thread is done!");
Console.ReadLine();
}
private static void Add(object data)
{
Console.WriteLine("ID of thread in Add(): {0}", Thread.CurrentThread.ManagedThreadId);
AddParms ap = (AddParms)data;
Console.WriteLine("{0} + {1} = {2}", ap.A, ap.B, ap.A + ap.B);
}发布于 2015-07-25 08:32:22
waitHandle.WaitOne();此行导致执行停止,直到设置了等待句柄。提供的代码从不设置等待句柄,因此代码会无限期地阻塞。
https://stackoverflow.com/questions/31621432
复制相似问题