CancellationTokenSource.Cancel正在抛出异常

时间:2014-03-17 16:21:22

标签: c# .net task wait cancellationtokensource

当我阅读文档时,CancellationTokenSource.Cancel不应该抛出异常 CancellationTokenSource.Cancel

在cts.Cancel()的调用之下;导致(不抛出)OperationCanceledException 我对此非常有信心,好像我评论该行,然后没有抛出最后一次OperationCanceledException。

当cts.Cancel行激活时,抛出异常的行是t2.Wait(token);
如果我在cts.Cancel()中有延迟;然后t2.Wait(令牌);一旦调用该行,就不会抛出异常 t2.Wait(令牌);只运行cts.Cancel()时抛出异常 这是正确的行为吗? 如果它是一致的那么我可以忍受它但我不希望cts.Cancel引起异常。
我显然很困惑 - 我只是想了解这种行为,所以我觉得把它带到生产环境很舒服 现在我正在使用BackGroundWorker这样做,我想我可以更容易地遵循和维护使用等待和取消。

if(token.IsCancellationRequested || ctr == 100000000)
仍然投掷ctr == 100000000
我仍然看到内部的OperationCanceledException被捕获,外部(下层)根本没有被抛出。

此代码有问题吗?
或者它是如何工作的?

没有在任务t2 = Task.Run中的try catch我得到了一个未被捕获的异常被抛出 我认为这会被t2上的try catch捕获。但是一次只有一个问题。

这是.NET 4.5上的控制台应用程序。

static void Main(string[] args)
{
    CancellationTokenSource cts = new CancellationTokenSource();
    CancellationToken token = cts.Token;

    Task.Run(() =>
    {
        Thread.Sleep(1000);
        cts.Cancel();  // this is thowing an exception that is caught on the last catch (OperationCanceledException Ex)
        // according to the documentation this is not supposed
        // if I comment out the cts.Cancel(); then the exeption is not thrown
        if (token.IsCancellationRequested)
            Console.WriteLine("Cancellation requested in Task {0}.",
                                Task.CurrentId);
        else
            Console.WriteLine("Cancellation Not requested in Task {0}.",
                                Task.CurrentId);

    }, token);

    //tried this but did not help
    //Task.Run(() =>
    //{
    //    //Thread.Sleep(1000);
    //    if (token.IsCancellationRequested)
    //        Console.WriteLine("Cancellation requested in Task {0}.",
    //                            Task.CurrentId);
    //}, token);
    //Thread.Sleep(1000);
    ////cts.Cancel();


    Task t2 = Task.Run(() =>
    {
        try
        {
            Console.WriteLine("Task t2 started Int32.MaxValue = " + Int32.MaxValue.ToString());
            Thread.Sleep(4000);
            for (int ctr = 0; ctr < Int32.MaxValue; ctr++)
            {
                if (ctr < 100 || ctr % 100000000 == 0)
                {
                    Console.WriteLine(ctr.ToString());

                }
                if (token.IsCancellationRequested || ctr == 100000000)  //  || ctr == 100000000
                {
                    Console.WriteLine("ThrowIfCancellationRequested in t2 Task  {0}.",
                                Task.CurrentId);
                    throw new OperationCanceledException(token);
                    //token.ThrowIfCancellationRequested();
                }
            }
            Console.WriteLine("Task {0} finished.",
                            Task.CurrentId);
        }
        catch (OperationCanceledException Ex)
        {
            //Console.WriteLine(Ex.ToString());
            Console.WriteLine("OperationCanceledException in Task t2 {0}: The operation was cancelled.",
                                Task.CurrentId);
        }
        catch (Exception Ex)
        {
            Console.WriteLine("Task t2 = Task.Run Exception Ex" + Ex.Message);
        }               
    });
    try
    {
        Console.WriteLine("t2.Wait a");
        t2.Wait(token);
    }
    catch (AggregateException e)
    {
        Console.WriteLine("AggregateException");
        foreach (var v in e.InnerExceptions)
            Console.WriteLine(e.Message + " " + v.Message);
    }
    catch (OperationCanceledException Ex)
    {
        //Console.WriteLine(Ex.ToString());
        Console.WriteLine("OperationCanceledException in Task {0}: The operation was cancelled.",
                            t2.Id);
    }
    catch (Exception Ex)
    {
        Console.WriteLine(Ex.ToString());
    }
    Console.WriteLine("end");
    Console.ReadLine();
}

3 个答案:

答案 0 :(得分:3)

是的,这是预期的行为: 取消令牌的Task.Wait的重载会等到:

  • 您正在等待的任务完成
  • 传入的取消令牌已取消。

在后一种情况下,当Task.Wait观察到传入的令牌已被取消时,它会抛出一个&#39; OperationCancelledException&#39;。这是您在案例中触发异常时的调用堆栈

    mscorlib.dll!System.Threading.CancellationToken.ThrowOperationCanceledException() Line 505 + 0x4d bytes C#
    mscorlib.dll!System.Threading.ManualResetEventSlim.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Line 642 C#
    mscorlib.dll!System.Threading.Tasks.Task.SpinThenBlockingWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Line 3284 + 0xf bytes  C#
    mscorlib.dll!System.Threading.Tasks.Task.InternalWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Line 3223 + 0x10 bytes C#
    mscorlib.dll!System.Threading.Tasks.Task.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Line 3129 + 0xc bytes  C#
    mscorlib.dll!System.Threading.Tasks.Task.Wait(System.Threading.CancellationToken cancellationToken) Line 3064 + 0xe bytes   C#
>   ConsoleApplication1.exe!ConsoleApplication1.Program.Main(string[] args) Line 82 + 0x15 bytes    C#

关于你问题的第二部分: 如果你   - 在t2和t2中删除了try / catch   - 你从未取消过令牌,

然后你最终会在你的AggregateException处理程序中登陆(因为对t2.Wait的调用会抛出一个AggregateException,其中有一个内部异常,即OperationCanceledException你扔了。

答案 1 :(得分:1)

我可能错了,但是你得到这个例外的原因是因为你取消了放置cancel语句的相同任务。 operationCanceledException MSDN文档说明如下: -

The exception that is thrown in a thread upon cancellation of an operation that the thread was executing.

您是否可以尝试将第一个任务调用修改为以下内容并检查是否仍然出现异常: -

Task.Run(() =>
    {
        Thread.Sleep(1000);
        if (token.IsCancellationRequested)
            Console.WriteLine("Cancellation requested in Task {0}.",
                                Task.CurrentId);
    }, token);
cts.Cancel();

taskCancellationSource的有用链接: -

http://johnbadams.wordpress.com/2012/03/10/understanding-cancellationtokensource-with-tasks/

答案 2 :(得分:1)

t2.Wait(token);应该在大约1秒之后抛出,因为token被取消了。

该行

cts.Cancel();
如果"Cancellation requested in Task {0}."按照你说的那样打印到控制台,则不可能抛出