如何使用CancellationToken取消任务?

时间:2016-02-24 19:04:59

标签: c# task-parallel-library task cancellationtokensource

所以我有这段代码:

//CancelationToken
CancellationTokenSource src = new CancellationTokenSource();
CancellationToken ct = src.Token;
ct.Register(() => Console.WriteLine("Abbruch des Tasks"));
//Task
Task t = new Task(() =>
{
    System.Threading.Thread.Sleep(1000);
    if (ct.IsCancellationRequested)
    {
        try
        {
            //Throw
            ct.ThrowIfCancellationRequested();                        
        }
        catch (OperationCanceledException)
        {
            Console.WriteLine(
                "ThrowIfCancellationRequested() liefert eben eine Exception");
        }
    }             

}, ct);
//Run Task and Cancel
t.Start();
src.CancelAfter(350);
t.Wait();

// Get Information
Console.WriteLine("Canceled: {0} . Finished: {1} . Error: {2}",
                    t.IsCanceled, t.IsCompleted, t.IsFaulted);

所以在这种情况下我取消了我的任务,但我的输出到底是: “已取消:错误。已完成:正确。错误:错误”

在我看来,它应该是“取消:真实。完成:错误”。 为什么我得到这个结果?因为我试图抓住异常?

我在没有try-catch块的情况下尝试过,但是由于OperationCanceledException,我的程序停止了。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:5)

您正在吞噬异常,因此任务在您实际处理异常时被标记为已完成,并且不会向外传播。

相反,不要在委托中捕获异常,将其捕获到外面:

void Main()
{
    CancellationTokenSource src = new CancellationTokenSource();
    CancellationToken ct = src.Token;
    ct.Register(() => Console.WriteLine("Abbruch des Tasks"));

    Task t = Task.Run(() =>
    {
        System.Threading.Thread.Sleep(1000);
        ct.ThrowIfCancellationRequested();
    }, ct);

    src.Cancel();
    try
    {
        t.Wait();
    }
    catch (AggregateException e)
    {
        // Don't actually use an empty catch clause, this is
        // for the sake of demonstration.
    }

    Console.WriteLine("Canceled: {0} . Finished: {1} . Error: {2}",
                       t.IsCanceled, t.IsCompleted, t.IsFaulted);
}