【发布时间】:2015-08-08 14:46:48
【问题描述】:
我试图了解 .NET 4.0 中的 UnobservedTaskException 问题,因此我编写了以下代码
TaskScheduler.UnobservedTaskException += (sender, eventArgs) => Console.WriteLine("unobserved");
Task.Factory.StartNew(() => { throw new Exception(); }, TaskCreationOptions.LongRunning);
using (var autoResetEvent = new AutoResetEvent(false))
{
autoResetEvent.WaitOne(TimeSpan.FromSeconds(10));
}
Console.WriteLine("Collecting");
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Still working ");
Console.ReadKey();
Console.WriteLine("Still working ");
Console.WriteLine("Still working ");
Console.ReadKey();
UnobservedTaskException 被触发,然后我的应用程序继续工作。但是根据MSDN 应该杀死该进程。谁能告诉我为什么?
【问题讨论】:
-
如何确保它在 .Net 4.0 上运行?
标签: c# .net-4.0 task-parallel-library task unobserved-exception