CodeGo.net> ThrowUnobservedTaskExceptions不工作
作者:互联网
我已经在Winform中创建了一个测试应用程序,以学习Task(c#)中的异常处理.目标框架是4.0.以下是我的异常处理代码
var task = Task.Factory.StartNew<DataTable>(() => getDataTable(Convert.ToInt32 (this.textBoxOptionVal.Text)));
task.ContinueWith(t =>
{
this.dataGridViewData.DataSource = t.Result as DataTable;
this.textBoxRetVal.Text = "Success" ;
},
CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext());
task.ContinueWith(t =>
{
// Update UI (and UI-related data) here: failed status.
// t.Exception contains the occured exception.
AggregateException aggregateException = t.Exception;
aggregateException.Handle(exception => true);
this.dataGridViewData.DataSource = null;
this.textBoxRetVal.Text = "Exception Thrown";
},
CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext());
这很好.如果getDataTable方法抛出任何异常,则将执行continuewith OnlyOnFaulted代码块.但是后来我注释掉了ContinueWith OnlyOnfaulted块中的所有语句,还添加了一个包含以下内容的配置文件
<?xml version="1.0"?>
<configuration>
<runtime>
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
但是问题甚至出在现在,当我运行代码并从getDataTable方法引发异常时,该异常是swallon并且该过程没有终止.可能是什么原因?请帮忙.
解决方法:
仅当任务由垃圾收集器完成时才检查任务是否存在未观察到的异常:
In the .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.
https://msdn.microsoft.com/en-us/library/jj160346
这是触发此行为的示例程序:
Program.cs
using System;
using System.Threading;
using System.Threading.Tasks;
namespace UnobservedTaskException
{
class Program
{
static void Main()
{
Console.WriteLine("Runtime Version: {0}", Environment.Version);
var task = Task.Factory.StartNew<int>(() =>
{
throw new Exception("Calculation in task failed");
});
while (!task.IsFaulted)
{
Thread.Sleep(1);
}
task = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>
</configuration>
输出量
Runtime Version: 4.0.30319.42000
Unhandled Exception: System.AggregateException: 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. ---> System.Exception: Calculation in task failed
at UnobservedTaskException.Program.<Main>b__0() in Program.cs:line 15
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of inner exception stack trace ---
at System.Threading.Tasks.TaskExceptionHolder.Finalize()
请注意,即使文档指出.NET Framework 4的默认行为是根据未观察到的Task异常终止进程,我仍然必须设置< ThrowUnobservedTaskExceptions enabled =“ true” />.使过程消亡.
标签:exception,task,c,net-4-0 来源: https://codeday.me/bug/20191029/1962733.html