如何使用ContinueWith处理任务取消?

时间:2017-09-05 12:28:32

标签: c# .net asynchronous task-parallel-library cancellationtokensource

我有以下例子:

public void Run()
{
    var ctc = new CancellationTokenSource();

    try
    {
        DoAsync(ctc).Wait();

        Console.WriteLine("Done");
    }
    catch (AggregateException exception)
    {
        Console.WriteLine("Inside try-catch block");
        Console.WriteLine();
        Console.WriteLine(exception);

        exception.Handle(ex =>
        {
            Console.WriteLine(ex.Message);
            return true;
        });
    }
}

private async Task DoAsync(CancellationTokenSource ctc)
{
    Console.WriteLine("DoAsync started");

    await Task.Run(() =>
                Console.WriteLine("DoAsync Run"),
                ctc.Token
            )
            .ContinueWith(antecedent =>
                Console.WriteLine("DoAsync Run cancelled"),
                TaskContinuationOptions.OnlyOnCanceled
            );

    Console.WriteLine("DoAsync finished");
}

我已经创建了一个方法( DoAsync ),它可以执行一些异步工作,并且可以随时取消。

如您所见,Task.Run获取取消令牌。出于这个原因,我使用 continuationOptions = TaskContinuationOptions.OnlyOnCanceled 创建了延续任务。

因此,我预计只有在请求取消时才会调用继续任务,而在其他情况下会被忽略。

但是在 ContinueWith 返回的实现任务中,当前一个任务没有被取消时会引发异常:

DoAsync started
DoAsync Run

Inside try-catch block
System.AggregateException...

A task was canceled.

我可以通过添加另一个 ContinueWith 来解决这个问题,如下例所示:

await Task.Run(() =>
        Console.WriteLine("DoAsync Run"),
        ctc.Token
    )
    .ContinueWith(antecedent =>
        Console.WriteLine("DoAsync Run cancelled"),
        TaskContinuationOptions.OnlyOnCanceled
    )
    .ContinueWith(antecedent => { });

此代码不会抛出任何异常。

但我可以使用单个 ContinueWith 正确处理取消吗?

1 个答案:

答案 0 :(得分:2)

ContinueWith的评论具体说明:

  

如果未满足通过 continuationOptions 参数指定的继续标准,则将取消继续任务而不是已安排。

由于您为先行者指定的标准未得到满足(即,它没有被取消),因此继续被设置为取消。您等待已取消的任务,因此导致DoAsync错误,操作取消了异常。

相关问题