如何判断线程是否成功退出?
根据 ThreadState 的 MSDN 文档,已停止可以通过以下两种方式之一进入状态:线程退出或线程被中止。
是否有某种机制可以判断线程是否通过正常退出而进入 Stopped 状态? 谢谢!
According to the MSDN Documentation for ThreadState, the Stopped state can be entered by one of two ways: the thread exiting, or the thread being aborted.
Is there some mechanism for telling whether a thread has entered the Stopped state by exiting normally? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
线程可以通过多种方式达到停止状态:
我不知道您是否想要区分所有三种状态,但如果您真正感兴趣的是线程是否成功完成,我建议使用某种共享数据结构(同步字典可以工作)线程的主循环在终止时更新。 您可以使用 ThreadName 属性作为此共享字典中的键。 对终止状态感兴趣的其他线程可以从此字典中读取以确定线程的最终状态。
详细查看 MSDN 文档后,您应该能够使用 ThreadState 属性区分外部中止的线程。 当线程响应 Abort() 调用时,应将其设置为 ThreadState.Aborted。 但是,除非您可以控制运行的线程代码,否则我认为您无法区分刚刚退出其 main 方法的线程和因异常终止的线程。
但请记住,如果您控制启动线程的代码,您始终可以替换自己的方法,该方法在内部调用运行主线程逻辑并注入异常检测的代码(如上所述)那里。
A thread can reach the Stopped state in several ways:
I don't know if you are looking to differentiate between all three states, but if all you are really interested in is whether the thread completed successfully, I would suggest using a shared data structure of some kind (a synchronized dictionary would work) that the main loop of a thread updates as it terminates. You can use the ThreadName property as the key in this shared dictionary. Other threads that are interested in termination state, could read from this dictionary to determine the final status of the thread.
After looking a bit more at the MSDN documentation, you should be able to differentiate an externally aborted thread using the
ThreadState
property. This should be set toThreadState.Aborted
when a thread responds to the Abort() call. However, unless you have control of the thread code that is run, I don't think that you can differentiate between a thread that just exited its main method, vs one that terminated with an exception.Keep in mind however, if you control the code that starts the thread, you can always substitute your own method that internally calls the code that runs the main thread logic and inject exception detection (as I described above) there.
是您想要监视代码的线程吗? 如果是这样,您可以将整个事情包装在一个类中,并在完成时引发一个事件或使用 WaitHandle (我将使用 ManualResetEvent)来表示完成。 -- 完全封装后台逻辑。 您还可以使用此封装来捕获异常,然后引发事件。
像这样的东西:
注意:您需要一些代码来防止 Start 被调用两次,并且您可能需要添加一个状态属性来将状态传递给您的线程。
这是一个控制台应用程序,演示了此类的用法:
Is the thread that you want to monitor your code? If so, you could wrap the whole thing in a class, and either raise an event when you finish or use a WaitHandle (I would use a ManualResetEvent) to signal completion. -- completely encapsulate the background logic. You can also use this encapsulation to capture an exception and then raise an event.
Something like this:
Note: you need some code to keep Start from being called twice, and you might want to add a state property for passing state to your thread.
Here's a console app that demonstrates the use of this class:
您可能想查看BackgroundWorker 类。 它有一个线程完成时的通用事件处理程序。 在那里您可以检查线程是否由于错误而完成、因为它被取消或因为它成功完成。
You may want to look at the BackgroundWorker class. It has a generic event handler for when the thread completes. In there you can check if the thread completed due to an error, because it was canceled or because it finished successfully.
假设主线程需要等待工作线程成功完成,我通常使用ManualResetEvent。 或者,对于多个工作线程,Parallels Extensions 中的新 CountDownEvent,例如 所以。
Assuming the main thread needs to wait for the worker thread to complete successfully, I usually use ManualResetEvent. Or, for multiple worker threads, the new CountDownEvent from Parallels Extensions, like so.
我使用 CancellationTokenSource 来要求线程正常退出。
然后,我使用 var exitedProperly = _thread.Join(TimeSpan.FromSeconds(10); 等待线程退出。
如果
exitedProperly==false
,我会将错误推送到 我主要在使用
Dispose()
函数时使用此模式,并且尝试清理我创建的所有线程。I use a
CancellationTokenSource
to ask the thread to exit gracefully.I then use
var exitedProperly = _thread.Join(TimeSpan.FromSeconds(10);
which waits for the thread to exit.If
exitedProperly==false
, I push an error into the log.I use this pattern mainly when I am in the
Dispose()
function, and I am trying to clean up any threads I have created.线程只能通过调用 Thread.Abort() 来中止,这会导致 ThreadAbortException,因此通过异常处理,您应该能够确定正常退出与中止退出。
A thread can only be aborted by calling Thread.Abort(), which results in a ThreadAbortException, so through exception handling you should be able to determine a normal exit vs an aborted exit.