我完全困惑了。我确信如果线程中存在未捕获的异常,.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();
}
发生这种情况是因为BeginInvoke
uses ThreadPool
内部和何时ThreadPool
任何未处理的异常都将是沉默失败。但是如果你使用a.EndInvoke
那么未处理的异常将被抛出EndInvoke
method.
注:如João Angelo
表示使用ThreadPool
方法直接“像ThreadPool.QueueUserWorkItems
and UnsafeQueueUserWorkItem
" 将在 2.0 及以上抛出异常。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)