<ThrowUnobservedTaskExceptions > 元素<ThrowUnobservedTaskExceptions> Element

指定未经处理的任务异常是否应终止正在运行的进程。Specifies whether unhandled task exceptions should terminate a running process.

<configuration><configuration>
<运行时 ><runtime>
<ThrowUnobservedTaskExceptions><ThrowUnobservedTaskExceptions>

语法Syntax

<ThrowUnobservedTaskExceptions  
   enabled="true|false"/>  

特性和元素Attributes and Elements

下列各节描述了特性、子元素和父元素。The following sections describe attributes, child elements, and parent elements.

特性Attributes

特性Attribute 描述Description
enabled 必需的特性。Required attribute.

指定未经处理的任务异常是否应终止正在运行的进程。Specifies whether unhandled task exceptions should terminate the running process.

enabled 特性enabled Attribute

Value 描述Description
false 不会终止正在运行的进程的未经处理的任务异常。Does not terminate the running process for an unhandled task exception. 这是默认设置。This is the default.
true 终止正在运行的进程的未经处理的任务异常。Terminates the running process for an unhandled task exception.

子元素Child Elements

无。None.

父元素Parent Elements

元素Element 描述Description
configuration 公共语言运行时和 .NET Framework 应用程序所使用的每个配置文件中的根元素。The root element in every configuration file used by the common language runtime and .NET Framework applications.
runtime 包含有关运行时初始化选项的信息。Contains information about runtime initialization options.

备注Remarks

如果与关联的异常Task未观察,没有任何Wait未附加操作,父级,和Task.Exception属性未读取任务异常被视为未观察到。If an exception that is associated with a Task has not been observed, there is no Wait operation, the parent is not attached, and the Task.Exception property was not read the task exception is considered to be unobserved.

在中.NET Framework 4.NET Framework 4,也可由默认情况下,如果Task具有未观察到异常进行垃圾收集,终结器引发异常,并终止进程。In the .NET Framework 4.NET Framework 4, by default, if a Task that has an unobserved exception is garbage collected, the finalizer throws an exception and terminates the process. 终止进程取决于垃圾回收和终止的时间。The termination of the process is determined by the timing of garbage collection and finalization.

为了简化开发人员能够编写基于任务的异步代码.NET Framework 4.5.NET Framework 4.5更改此默认行为为未观察到异常。To make it easier for developers to write asynchronous code based on tasks, the .NET Framework 4.5.NET Framework 4.5 changes this default behavior for unobserved exceptions. 未观察到的异常仍会导致UnobservedTaskException事件被引发,但默认情况下,该过程不会终止。Unobserved exceptions still cause the UnobservedTaskException event to be raised, but by default, the process does not terminate. 相反,该异常被忽略后引发该事件,而不考虑是否将事件处理程序观察异常。Instead, the exception is ignored after the event is raised, regardless of whether an event handler observes the exception.

在中.NET Framework 4.5.NET Framework 4.5,可以使用 <ThrowUnobservedTaskExceptions > 元素若要启用应用程序配置文件中.NET Framework 4.NET Framework 4引发异常的行为。In the .NET Framework 4.5.NET Framework 4.5, you can use the <ThrowUnobservedTaskExceptions> element in an application configuration file to enable the .NET Framework 4.NET Framework 4 behavior of throwing an exception.

此外可以通过以下方式之一中指定的异常行为:You can also specify the exception behavior in one of the following ways:

  • 通过设置环境变量COMPlus_ThrowUnobservedTaskExceptions(set COMPlus_ThrowUnobservedTaskExceptions=1)。By setting the environment variable COMPlus_ThrowUnobservedTaskExceptions (set COMPlus_ThrowUnobservedTaskExceptions=1).

  • 通过设置注册表 DWORD 值 ThrowUnobservedTaskExceptions = 1 在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\。NETFramework 键。By setting the registry DWORD value ThrowUnobservedTaskExceptions = 1 in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework key.

示例Example

下面的示例演示如何启用任务中的异常引发使用应用程序配置文件。The following example shows how to enable the throwing of exceptions in tasks by using an application configuration file.

<configuration>   
    <runtime>   
        <ThrowUnobservedTaskExceptions enabled="true"/>   
    </runtime>   
</configuration>  

示例Example

下面的示例演示如何从任务引发未观察到的异常。The following example demonstrates how an unobserved exception is thrown from a task. 作为已发布的程序才能正常工作,必须运行代码。The code must be run as a released program to work correctly.

using System;
using System.Threading;
using System.Collections.Generic;
using System.Threading.Tasks;

//Use the following config settings to enable the throwing of unobserved exceptions.
//    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
//    <ThrowUnobservedTaskExceptions enabled="true"/>

public class Example
{
    static void Main()
    {
        Task.Run(() => { throw new InvalidOperationException("test"); });
        while (true)
        {
            Thread.Sleep(100);
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}
Imports System.Threading
Imports System.Threading.Tasks

'Use the following config settings to enable the throwing of unobserved exceptions.
'    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
'    <ThrowUnobservedTaskExceptions enabled="true"/>

Public Class Example
    Shared Sub Main()
        Task.Run(Sub() Throw New InvalidOperationException("test"))
        Do
            Thread.Sleep(100)
            GC.Collect()
            GC.WaitForPendingFinalizers()
        Loop
    End Sub
End Class

请参阅See also