【发布时间】:2013-05-23 12:38:20
【问题描述】:
我对下面的代码 sn-p 感到很困惑。
//Console.WriteLine("Main thread: Doing other work here...");
ThreadPool.QueueUserWorkItem(
state =>
{
for (int i = 0; i < 10; i++)
{
Thread.Sleep(200);
Console.WriteLine("In computeBoundOp: state={0} " + i, state);
}
}, 5);
//Console.WriteLine("Hit <Enter> to end this program...");
Console.ReadKey();
实际上,线程池线程不会打印任何内容,除非我在 ThreadPool 部分之前或之后取消注释“Console.WriteLine()”。(请注意,我什至在主线程末尾有一个 readkey)
否则,要让后台线程打印一些东西,我必须使用手动重置句柄让主线程等待线程完成。
知道这是怎么发生的吗?
谢谢。
【问题讨论】:
-
它适用于我的笔记本电脑
-
它也适用于我的机器。
标签: c# .net multithreading