拿这个帖子:
Thread thread = new Thread(delegate()
{
//Code
});
thread.Start();
它应该在thread.Start();
或内部:
Thread thread = new Thread(delegate()
{
try
{
//Code
}
catch (Exception)
{
//Code
}
});
答案 0 :(得分:6)
将内部或外部放置完全不同。
如果您将它们放在thread.Start()
电话周围,您可以检测到(根据此页面:http://msdn.microsoft.com/en-us/library/system.threading.thread.start(v=vs.71).aspx)
如果你把它放进去,你将检测你将在你的线程中运行的代码中的异常。所以你想要的任何异常。
答案 1 :(得分:3)
应该在委托内部处理与委托中的逻辑有关的异常。
thread.Start()
本身只能投掷ThreadStateException
或OutOfMemoryException
。
答案 2 :(得分:2)
Preventing silent thred termination
它解释了将try catch放在委托中。它还谈到了如果需要你做最后清理。
答案 3 :(得分:1)
如上所述,如果错误发生在委托代码中,那么将try-catch放在那里并记录异常。或者,如果您希望将该异常传递回原始线程,请使用异步委托(调用EndInvoke会将异常重新引发到调用线程或使用Background worker并订阅RunWorkerCompleted事件(这在事件args中具有error属性)