簡體   English   中英

處理任務拋出的異常

[英]Handle exception thrown by a task

我有一個在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異常處理的內容,但我不明白為什么它沒有被捕獲。 拜托,你能幫幫我嗎?

任務完成后,您可以檢查其Exception屬性。 您還有Status和IsCompleted屬性,可能對您有用...

檢查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
    }
}

就像使用同步代碼一樣(但這不是一個實際的同步調用)

我相信問題的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM