TaskScheduler.UnobservedTaskException 事件
定义
出错的任务中未观察到的异常将触发异常呈报策略时出现,默认情况下会终止进程。Occurs when a faulted task's unobserved exception is about to trigger exception escalation policy, which, by default, would terminate the process.
public:
static event EventHandler<System::Threading::Tasks::UnobservedTaskExceptionEventArgs ^> ^ UnobservedTaskException;
public static event EventHandler<System.Threading.Tasks.UnobservedTaskExceptionEventArgs> UnobservedTaskException;
public static event EventHandler<System.Threading.Tasks.UnobservedTaskExceptionEventArgs>? UnobservedTaskException;
[add: System.Security.SecurityCritical]
[remove: System.Security.SecurityCritical]
public static event EventHandler<System.Threading.Tasks.UnobservedTaskExceptionEventArgs> UnobservedTaskException;
member this.UnobservedTaskException : EventHandler<System.Threading.Tasks.UnobservedTaskExceptionEventArgs>
[<add: System.Security.SecurityCritical>]
[<remove: System.Security.SecurityCritical>]
member this.UnobservedTaskException : EventHandler<System.Threading.Tasks.UnobservedTaskExceptionEventArgs>
Public Shared Custom Event UnobservedTaskException As EventHandler(Of UnobservedTaskExceptionEventArgs)
事件类型
- 属性
注解
此应用程序域范围内的事件提供一种机制来防止异常升级策略 (默认情况下,它会终止进程) 触发。This application domain-wide event provides a mechanism to prevent exception escalation policy (which, by default, terminates the process) from triggering.
为了使开发人员可以更轻松地根据任务编写异步代码,.NET Framework 4.5 更改未观察到异常的默认异常行为。To make it easier for developers to write asynchronous code based on tasks, the .NET Framework 4.5 changes the default exception behavior for unobserved exceptions. 尽管未观察到异常仍会引发 UnobservedTaskException 异常,但默认情况下该过程不会终止。Although unobserved exceptions still raise the UnobservedTaskException exception, the process does not terminate by default. 相反,引发事件后,运行时将处理异常,而不管事件处理程序是否观察到异常。Instead, the exception is handled by the runtime after the event is raised, regardless of whether an event handler observes the exception. 可以配置此行为。This behavior can be configured. 从 .NET Framework 4.5 开始,可以使用 <ThrowUnobservedTaskExceptions> 配置元素恢复为 .NET Framework 4 的行为,并终止进程:Starting with the .NET Framework 4.5, you can use the <ThrowUnobservedTaskExceptions> configuration element to revert to the behavior of the .NET Framework 4 and terminate the process:
<configuration>
<runtime>
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>
</configuration>