处理任务抛出的异常

问题描述 投票:0回答:3

我有一个在WPF中运行长时间操作的任务:

Task t = Task.Factory.StartNew(() =>
{
    try
    {
        process(cancelTokenSource.Token, CompressionMethod, OpInfo);
    }
    catch (OperationCanceledException)
    {
        logger.Info("Operation cancelled by the user");
    }
}, cancelTokenSource.Token);

try
{
    t.Wait();
}
catch (AggregateException ae)
{
    int i = 0;
}     


private void process(CancellationToken token, CompressionLevel level, OperationInfo info)
{
    // check hash
    if (ComputeHash)
    {
        logger.Info("HASH CHECKING NOT IMPLEMENTED YET!");
        MessageBox.Show(this,"HASH CHECKING NOT IMPLEMENTED YET!", "WARNING", MessageBoxButton.OK, MessageBoxImage.Warning);
    }
    token.ThrowIfCancellationRequested();
    UserMsgPhase = "Operation finished";

    return info;
}

问题是“MessageBox.Show”抛出异常并且未在“catch(AggregateException ae)”中捕获。我一直在阅读有关TPL异常处理的内容,但我不明白为什么它没有被捕获。拜托,你能帮帮我吗?

c# exception exception-handling task-parallel-library
3个回答
2
投票

任务完成后,您可以检查其Exception属性。您还有Status和IsCompleted属性,可能对您有用...


0
投票

检查Task.Exception。如果您的任务是键入的(返回结果),那么访问myTask.Result将抛出此异常。

此外,如果您运行的是.Net 4.5,则可以使用async / await。

举个例子:

public async void MyButton_OnClick(object sender, EventArgs e)
{
    try
    {
        Task t = ...your task...;
        var myResult = await t; // do whatever you like with your task's result (if any)
    }catch
    {
        // whatever you need
    }
}

就像使用同步代码一样(但这不是一个实际的同步调用)


0
投票

我相信问题的process方法是一个Task,所以看起来它可以以不同的方式实现:

  1. 您可以将该过程实现为Task,然后您将在task-parent中拥有一个task-child。
  2. 然后你可以使用TaskCreationOptions.AttachedToParent选项。

根据Stephen Toub,使用AttachedToParent将有助于通知父任务捕获的子任务异常:

来自故障子节点的任何异常都将传播到父Task(除非父Task在完成之前观察这些异常)。

例:

我省略了取消令牌部分,以使其更简单。

Task t = Task.Factory.StartNew(() =>
{
    var process = new Task(() =>
    {
        //Copy here the process logic. 
    }, TaskCreationOptions.AttachedToParent);

    //*Private failure handler*.

    process.start();
});

try
{
    t.Wait();
}
catch (AggregateException ae)
{
    //handle exceptions from process.
}

此外,您可以添加一个私有故障处理程序,如:

//*Private failure handler*.
var failHandler = child.ContinueWith(t =>
{
    //Oops, something went wrong...
}, TaskContinuationOptions.AttachedToParent|TaskContinuationOptions.OnlyOnFaulted);
© www.soinside.com 2019 - 2024. All rights reserved.