在.NET4不可观测任务异常 [英] Unobserved task exceptions in .NET4

查看:190
本文介绍了在.NET4不可观测任务异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据一些文章和博客一个code像下面应该导致.NET 4

According to some articles and blogs a code like the following one should lead to an exception in .NET 4

static void Main(string[] args)
    {
        Task.Factory.StartNew(() => { throw new Exception(); });
        Thread.Sleep(1000);
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Console.WriteLine("Completed"); 
    }

预期的异常:

Expected exception:

未处理的异常:System.AggregateException:任务的异常(S),并没有观察到无论是等待的任务或访问其Exception属性。其结果是,未观察到的异常重新抛出了终结器线程。 ---> System.InvalidOperationException:操作无效由于对象的当前状态

Unhandled Exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.

但它不`吨。 .NET在我的电脑4应用程序行为像.NET 4.5:

But it doesn`t. .NET 4 applications on my PC behave like .NET 4.5:

  • 他们不`吨默认情况下,抛出该异常聚集
  • 他们发现在配置文件中的以下设置:

< ThrowUnobservedTaskExceptions启用=真/>

< ThrowUnobservedTaskExceptions enabled="true"/>

看起来像.NET 4中已经修补,以获取.NET 4.5具有相同的行为。这是真的还是我有一些麻烦与我的配置? 或者有什么.NET4应用程序(不针对4.5)的行为这样,如果安装了4.5? 先谢谢了。

Looks like .NET 4 has been patched to get the same behavior that .NET 4.5 has. It it true or I have some troubles with my configuration? Or any .NET4 app (not targeting 4.5) will behave that way if 4.5 is installed? Thanks in advance.

推荐答案

我的猜测是,你的实际上的有关.NET 4.5上运行。熊在.NET 4.5中有效地安装在了.NET 4的顶部头脑即使你的应用程序是针对.NET 4,如果用户已经安装了.NET 4.5,你会得到新的行为。

My guess is that you're actually running on .NET 4.5. Bear in mind that .NET 4.5 is effectively installed over the top of .NET 4. Even if your application is targeted at .NET 4, if the user has installed .NET 4.5 you'll get the new behaviour.

这是完全有可能为用户真正只有.NET 4的安装,但...

It's entirely possible for a user to genuinely only have .NET 4 installed though...

这篇关于在.NET4不可观测任务异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆