UnobservedTaskException是不是杀死进程 [英] UnobservedTaskException is not killing the process
问题描述
我想了解.NET 4.0的UnobservedTaskException问题,所以我写了folloging代码
I am trying to understand the UnobservedTaskException issue in .NET 4.0 so I wrote the folloging code
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 的过程应该被杀死。谁能告诉我为什么吗?
UnobservedTaskException is fired and then my app just keeps working. However according to MSDN the process should be killed. Can anyone tell me why ?
推荐答案
如果您运行的机器,只有.NET 4.0上运行此代码安装它确实会崩溃
If you run this code on a machine that only has .Net 4.0 installed it will indeed crash.
由于所有版本的.Net 4.0以来都是就地即使你的目标你的应用程序.NET 4.0,将在更高版本的机器上运行更新有一个。
Since all the .Net versions since 4.0 were in-place updates even if you target your application for .Net 4.0 it will run on a later version on a machine that has one.
要得到,而在可以添加到您的app.config文件(如的 TaskScheduler.UnobservedTaskException事件):
To get the same behavior of .Net 4.0 while running on a later version you can add this to your app.config file (as described in TaskScheduler.UnobservedTaskException Event):
<runtime>
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>
这篇关于UnobservedTaskException是不是杀死进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!