任务的异常(s)的等候在工作或访问其Exception属性未观察到无论是。其结果是,未观察到的例外是 [英] 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

查看:2029
本文介绍了任务的异常(s)的等候在工作或访问其Exception属性未观察到无论是。其结果是,未观察到的例外是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么意思,如何解决?

我使用TPL任务。

整个误差


  

一个任务的异常(S)无论是在等待任务或访问其Exception属性未观察到。其结果是,未观察到的异常被终结器线程重新抛出。


  
  

在System.Threading.Tasks.TaskExceptionHolder.Finalize()


  
  

mscorlib程序



解决方案

如果您创建一个任务,你永远不要叫 task.Wait()或尝试检索任务&LT的结果; T> ,当任务被垃圾收集器收集,将推倒终止过程中您的应用程序。有关详细信息,请参阅MSDN对异常处理页面的TPL

在这里最好的办法是处理异常。这可以通过继续做 - 你可以附加一个持续的任务,然后登录/燕子/ etc中出现异常。这提供了一个干净的方式来登录任务异常,并且可写为一个简单的扩展方法,即:

 公共静态无效LogExceptions(该任务的任务)
{
    task.ContinueWith(T =>
    {
         变种aggException = t.Exception.Flatten();
         的foreach(在aggException.InnerExceptions VAR除外)
             LogException(例外);
    },
    TaskContinuationOptions.OnlyOnFaulted);
}

有了上面,你可以prevent撕裂了应用,并记录它,通过任何任务:

  Task.Factory.StartNew(()=>
   {
       // 做你的工作...
   })LogExceptions()。

另外,你可以订阅<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.unobservedtaskexception.aspx\">TaskScheduler.UnobservedTaskException并有处理它。

What does this mean and how to resolve it?

I am using TPL tasks.

The whole error

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.

at System.Threading.Tasks.TaskExceptionHolder.Finalize()

mscorlib

解决方案

If you create a Task, and you don't ever call task.Wait() or try to retrieve the result of a Task<T>, when the task is collected by the garbage collector, it will tear down your application during finalization. For details, see MSDN's page on Exception Handling in the TPL.

The best option here is to "handle" the exception. This can be done via a continuation - you can attach a continuation to the task, and log/swallow/etc the exception that occurs. This provides a clean way to log task exceptions, and can be written as a simple extension method, ie:

public static void LogExceptions(this Task task)
{
    task.ContinueWith( t =>
    {
         var aggException = t.Exception.Flatten();
         foreach(var exception in aggException.InnerExceptions)
             LogException(exception);
    }, 
    TaskContinuationOptions.OnlyOnFaulted);
}

With the above, you can prevent any task from tearing down the app, and logging it, via:

Task.Factory.StartNew( () => 
   { 
       // Do your work...
   }).LogExceptions();

Alternatively, you can subscribe to the TaskScheduler.UnobservedTaskException and handle it there.

这篇关于任务的异常(s)的等候在工作或访问其Exception属性未观察到无论是。其结果是,未观察到的例外是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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