[英]Observed Task Exception calling TaskScheduler.UnobservedTaskException
My App is .NET 4.5 and I have a event handler that logs all error on unobserved task exceptions. 我的应用程序是.NET 4.5,我有一个事件处理程序,用于记录所有未观察到的任务异常的错误。
TaskScheduler.UnobservedTaskException += (sender, e) => e.Exception.Handle(ex =>
{
logger.Error(e.Exception);
return false;
});
I return false, what shuts down the application. 我返回false,这将关闭应用程序。 Simulating the .NET 4.0 behavior. 模拟.NET 4.0行为。
My question is, why this code calls the event and consequently crash my app if I am observing the result? 我的问题是,如果我观察到结果,为什么这段代码会调用该事件并因此导致我的应用程序崩溃?
try
{
var resultado = httpClient.GetAsync('http://.....').Result;
}
catch (Exception ex)
{
logger.Error(ex);
}
UPDATE: After some debugging, I figure out that I had another Task when I didn´t called .Result and was causing the problem. 更新:经过一些调试后,我发现当我没有调用.Result并导致问题时,我还有另一个Task。
You're supposed to set UnobservedTaskExceptionEventArgs.Observed
to true. 您应该将UnobservedTaskExceptionEventArgs.Observed
设置为true。 Observing the result at this point has no effect, although one might reasonably believe that it does. 尽管有人可能会合理地相信这样做,但在这一点上观察结果没有任何效果。
经过一些调试后,我发现当我没有调用.Result并导致问题时,我还有另一个任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.