简体   繁体   中英

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.

TaskScheduler.UnobservedTaskException += (sender, e) => e.Exception.Handle(ex =>
{
    logger.Error(e.Exception);
    return false;
});

I return false, what shuts down the application. Simulating the .NET 4.0 behavior.

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.

You're supposed to set UnobservedTaskExceptionEventArgs.Observed to true. Observing the result at this point has no effect, although one might reasonably believe that it does.

经过一些调试后,我发现当我没有调用.Result并导致问题时,我还有另一个任务。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM