c#-4.0 – TPL异常处理

http://msdn.microsoft.com/en-us/library/dd997415.aspx

根据上面引用的文章,我试图处理continuatin任务中的异常.我在上面的文章中引用的例子是这样的:

var task1 = Task.Factory.StartNew(() =>
{
    throw new MyCustomException("Task1 faulted.");
})
.ContinueWith((t) =>
    {
        Console.WriteLine("I have observed a {0}",t.Exception.InnerException.GetType().Name);
    },TaskContinuationoptions.OnlyOnFaulted);

我的代码是:

Task<string> task = Task<string>.Factory.StartNew(() => process.StartTask(this));
task.ContinueWith(CloseDialog,TaskContinuationoptions.OnlyOnFaulted);

在StartTask中,我像示例一样抛出错误.我的期望是CloseDialog将执行,我可以检查该方法中的task.Exception,如示例所示.但是,当我抛出异常时,代码只会因未处理的异常而停止.我应该使用try / catch块吗?如果是这样,在哪里?
顺便说一句,我希望我的延续任务(CloseDialog)始终运行.我只是使用.OnlyOnFaulted,因为这是示例中显示内容.

解决方法

延续可以查明先行任务的异常属性是否由先行任务抛出异常.以下内容将NullReferenceException的结果打印到控制台
Task task1 = Task.Factory.StartNew (() => { throw null; });
Task task2 = task1.ContinueWith (ant => Console.Write(ant.Exception());

如果task1抛出异常并且延迟没有捕获/查询此异常,则认为它未处理并且应用程序死亡.使用continuation,通过Status关键字建立任务结果就足够了

asyncTask.ContinueWith(task =>
{
    // Check task status.
    switch (task.Status)
    {
        // Handle any exceptions to prevent UnobservedTaskException.             
        case TaskStatus.RanToCompletion:
            if (asyncTask.Result)
            {
                // Do stuff...
            }
            break;
        case TaskStatus.Faulted:
            if (task.Exception != null)
                mainForm.progressRightLabelText = task.Exception.InnerException.Message;
            else
                mainForm.progressRightLabelText = "Operation Failed!";
        default:
            break;
    }
}

如果不使用continuation,则必须等待try / catch块中的任务或在try / catch块中查询任务的Result

int x = 0;
Task<int> task = Task.Factory.StartNew (() => 7 / x);
try
{
    task.Wait();
    // OR.
    int result = task.Result;
}
catch (AggregateException aggEx)
{
    Console.WriteLine(aggEx.InnerException.Message);
}

希望这有帮助,即使它有点晚了,你现在知道所有的一切! :]

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...