任务取消的异常(ThrowForNonSuccess) [英] Task Cancelled Exception (ThrowForNonSuccess)

查看:4822
本文介绍了任务取消的异常(ThrowForNonSuccess)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从这个问题的延续:
任务Continuiation

This is a continuation from this question: Task Continuiation

我已经改变了我的code为答案,但现在我收到 TaskCancelledExceptions 当我尝试运行任务。

I have changed my code as in the answer, however now I am receiving TaskCancelledExceptions when I try to run tasks.

public virtual async Task RunAsync(TaskWithProgress task)
{
    Show();
    TaskIsRunning();
    await SetCompletedHandler(TaskComplete());
    await SetCancelledHandler(TaskCancelled())
    await SetFaultedHandler(TaskFaulted());
    await task;
    Close();
}

但是以下code不。我有点坚持,为什么。

however the following code does not. I am a bit stuck as to why.

public virtual Task RunAsync(TaskWithProgress task)
{
    Show();
    TaskIsRunning();
    SetCompletedHandler(TaskComplete());
    SetCancelledHandler(TaskCancelled())
    SetFaultedHandler(TaskFaulted());
    return task;
}

调用code主要涉及以下内容:

The calling code basically involves the following:

await progressDialog.RunAsync(task);

编辑:

我不取消的CancellationToken 任何地方,所以我不明白这是为什么抛出该异常。

I do not cancel a cancellationtoken anywhere so I can't see why this is throwing that exception.

三SetXXXHandler()方法,基本上是用不同的状态继续执行以下code:

The three SetXXXHandler() methods basically perform the following code with different continuation status:

task.ContinueWith(_ => action(), CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, this.Scheduler);

堆栈跟踪是在这里:

The Stack trace is here:

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at FugroDXExt.frmBaseProgressAsync.<RunAsync>d__7.MoveNext() in d:\C#\FugroDXExt\trunk\frmBaseProgressAsync.cs:line 92
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at FCP.Forms.frmProcessing.<mnuApplyCenteredSmoothing_ItemClick>d__34.MoveNext() in d:\C#\FCP\FCP\Forms\frmProcessing.cs:line 578

关闭()直接关闭的形式。如果删除该行同样的事情发生。

Close() simply closes the form. If I remove that line the same thing occurs.

推荐答案

您说 SetCancelledHandler 只是增加了一个延续的任务。我认为这是同一个任务 RunAsync 获取作为参数,虽然我不能用你的code如何 SetCancelledHandler 得到一个任务,继续对(我假设我们缺少一些code)。反正...

You say that SetCancelledHandler just adds a continuation to the task. I assume that's the same task RunAsync gets as a parameter although i can't tell by your code how SetCancelledHandler gets a task to continue on (I assume we're missing some code). Anyways...

您注册在任务完成时将运行任务3的延续,将被取消,出现故障。现在让我们假设原始任务运行完成成功不被取消。这意味着,你的延续2( OnCanceled OnFaulted )将无法运行,因为他们并不需要的人。顺便讲一个任务不是在 TPL运行是要取消它,而自动发生。

You register 3 continuations on a task that will run when the task completes, is canceled and is faulted. Now let's assume the original task ran to completion successfully without being canceled. That means that 2 of your continuations (OnCanceled and OnFaulted) will not run because they don't need to be. The way to tell a task to not run in the TPL is to cancel it, and that happens automatically.

您2 code段之间的区别在于,第一个你等待任务的延续,他们被取消这也解释了你的异常。关于第二个片段你不等待延续,只是运行成功完成原任务。

The difference between your 2 code snippets is that in the first one you await the task continuations, and they get canceled which explains your exception. On the second snippet you don't await the continuations, just the original task that successfully ran to completion.

P.S:我觉得第二个选项是比较合适的。你并不需要等待所有的延续。你想,让他们跑,如果他们需要。

P.S: I think the second option is more appropriate. You don't need to await all those continuations. You want to let them run if they need to.

TL; DR:您的await 已取消的后续任务。延续任务,而不是原来,是抛出一个异常的人。

TL;DR: You await a canceled continuation task. The continuation task, not the original, is the one that throws an exception.

这篇关于任务取消的异常(ThrowForNonSuccess)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆