Elegantly handle task cancellation

后端 未结 6 635
花落未央
花落未央 2021-01-03 18:15

When using tasks for large/long running workloads that I need to be able to cancel I often use a template similar to this for the action the task executes:

pu         


        
6条回答
  •  庸人自扰
    2021-01-03 18:43

    C# 6.0 has a solution for this..Filtering exception

    int denom;
    
    try
    {
         denom = 0;
        int x = 5 / denom;
    }
    
    // Catch /0 on all days but Saturday
    
    catch (DivideByZeroException xx) when (DateTime.Now.DayOfWeek != DayOfWeek.Saturday)
    {
         Console.WriteLine(xx);
    }
    

提交回复
热议问题