简体   繁体   English

观察到的任务异常调用TaskScheduler.UnobservedTaskException

[英]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.

相关问题 为什么TaskScheduler.UnobservedTaskException不触发? - Why is the TaskScheduler.UnobservedTaskException does not trigger? TaskScheduler.UnobservedTaskException - 引发前的高延迟 - TaskScheduler.UnobservedTaskException - High delay before raise system.threading.task - 为什么不发生TaskScheduler.UnobservedTaskException事件? 我能解决这个问题吗? - system.threading.task - why does TaskScheduler.UnobservedTaskException event not occur? Can I fix this? 永远不会触发TaskScheduler.UnobservedTaskException事件处理程序 - TaskScheduler.UnobservedTaskException event handler never being triggered 使用TaskScheduler.UnobservedTaskException可以避免进程被杀死吗? - Using TaskScheduler.UnobservedTaskException can avoid the process being killed? 任务线程中抛出的异常,未被UnobservedTaskException捕获 - Exception thrown in Task Thread, not caught by UnobservedTaskException 为什么未为引发异常的Task触发UnobservedTaskException事件处理程序? - Why is the UnobservedTaskException event handler not triggered for a Task which throws an exception? TaskScheduler_UnobservedTaskException是否可以捕获“ThrowIfCancellationRequested”? - Can “ThrowIfCancellationRequested” not be caught by TaskScheduler_UnobservedTaskException? 没有观察到任务的异常 - A Task's exception(s) were not observed Xamarin.Forms PopModalAsync:未观察到任务的异常 - Xamarin.Forms PopModalAsync: A Task's exception(s) were not observed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM