c# - CancellationTokenSource.Cancel 抛出异常

标签 c# .net task wait cancellationtokensource

正如我阅读文档 CancellationTokenSource.Cancel 不应该抛出异常。
CancellationTokenSource.Cancel

在 cts.Cancel() 调用下方;导致(不抛出)OperationCanceledException。
我对此非常有信心,就好像我注释掉那行然后不会抛出最后一个 OperationCanceledException 一样。

在 cts.Cancel 行处于事件状态时,抛出异常的行是 t2.Wait(token);
如果我有延迟 cts.Cancel();然后 t2.Wait(token);不会在调用该行时立即抛出异常。
t2.等待( token );仅在运行 cts.Cancel() 时抛出该异常。
这是正确的行为吗?
如果它是一致的,那么我可以接受它,但我不希望 cts.Cancel 导致异常。
我显然很困惑 - 我只是想了解行为,这样我就可以放心地将它带到生产环境中。
现在,我正在使用 BackGroundWorker 执行此操作,我认为我可以使用等待和取消来简化跟踪和维护。

if (token.IsCancellationRequested || ctr == 100000000)
仍然抛出 ctr == 100000000
我仍然看到内部 OperationCanceledException 被捕获并且外部(较低)根本没有被抛出。

这段代码有问题吗?
或者它应该如何工作?

如果没有 Task 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();
}

最佳答案

是的,这是预期的行为: 需要取消 token 的 Task.Wait 的重载会等到:

  • 您正在等待的任务完成
  • 传入的取消 token 被取消。

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

    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 中的 try/catch 和 - 你从未取消 token ,

那么你最终会在你的外部 AggregateException 处理程序中着陆(因为调用 t2.Wait 会抛出一个 AggregateException 与一个内部异常就是您抛出的 OperationCanceledException

关于c# - CancellationTokenSource.Cancel 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22459766/

相关文章:

c# - grpc:服务器重启后大约 15 秒无法恢复连接

c# - 垃圾收集器是否会在 .NET 中的异步调用期间销毁临时未引用的对象?

excel - 如何使用 "interact with desktop"从调度程序运行 Windows 2008 任务

c# - 任务可用的最大线程数

c# - WhenAny vs WhenAll vs WaitAll vs none,考虑到结果正在被立即使用

c# - 是否可以在调用 DbContext.SaveChanges 之前查询 Entity Framework ?

c# - MethodBase.IsConstructor 无法按照静态构造函数指定的方式工作

c# - 通过 SMTP 客户端发送电子邮件时出错

c# - 在 ASP.NET 5 中创建基于每个请求的 Controller /操作的格式化程序

c# - 从 LINQ 的 Any 方法返回 Task<bool>?