我感到十分困惑。我一直相信,如果在线程中有未捕获的异常,.NET会关闭整个应用程序域,但我从未测试过。
但是,我刚刚尝试了下面的代码,它并没有失败...... 请问有人能解释一下原因吗?
(在 .NET 4 和 3.5 中尝试过)
static void Main(string[] args)
{
Console.WriteLine("Main thread {0}", Thread.CurrentThread.ManagedThreadId);
Action a = new Action(() =>
{
Console.WriteLine("Background thread {0}", Thread.CurrentThread.ManagedThreadId);
throw new ApplicationException("test exception");
});
a.BeginInvoke(null, null);
Console.ReadLine();
}
ThreadPool.QueueUserWorkItem
方法并传递一个会抛出异常的方法时,异常将被框架捕获掉,但实际上在2.0及之后的版本中并不是这样。 - João Angelo