C. *_*oss 0 c# multithreading .net-2.0
我在服务结束时在ThreadPool线程上调用Thread.Join.线程中执行的代码与调用Thread.Join的时间大致相同,但是Join需要2分钟才能返回.为什么Thread.Join需要2分钟才能返回.
日志:
(2009-10-08 14:22:09) Inf: ProcessRequests - Interrupted, exiting.
(2009-10-08 14:22:09) Dbg: ProcessingDriver.Stop - Waiting on thread to exit.
(2009-10-08 14:24:10) Dbg: ProcessingDriver.Stop - Thread joined.
Run Code Online (Sandbox Code Playgroud)
代码:
WaitHandle.Set(); //Signal it's time to go home
LogManager.Logger.WriteLog(LOG_SOURCE, "Waiting on thread to exit.", LogType.Debug, 7);
ProcessingThread.Join(); //Wait for the thread to go home
LogManager.Logger.WriteLog(LOG_SOURCE, "Thread joined.", LogType.Debug, 7);
Run Code Online (Sandbox Code Playgroud)
Ree*_*sey 14
你不应该在ThreadPool线程上调用Thread.Join.
当你调用Thread.Join时,它会使你的主线程保持活动状态.线程池不会(必然)关闭其线程,直到程序终止.在你的情况下,你很幸运,它决定在几分钟的空闲时间后关闭该线程 - 但这不能保证.如果你抓错了线程池线程,它可能永远挂起.
如果您需要等待设置的任务,请使用ManualResetEvent来跟踪其完成情况.