CancellationTokenSource.CancelAfter not working

2019-02-25 08:28发布

I'm trying to implement some retry logic base on this post (but with tasks) Cleanest way to write retry logic?

The idea for the retry logic is to then to implement a second task that triggers the cancelation after a give amount of time

void Main()
{
    RetryAction(() => Sleep(), 500);
}

public static void RetryAction(Action action, int timeout)
{
    var cancelSource = new CancellationTokenSource();                
    cancelSource.CancelAfter(timeout);

    Task.Run(() => action(), cancelSource.Token);    
}

public static void Sleep() 
{
    System.Threading.Thread.Sleep(5000);
    "done".Dump();
}

The above is a linqPad snippet (thus the "done".Dump())

Any idea why the CancelAfter never works ?

标签: c# task linqpad
1条回答
叼着烟拽天下
2楼-- · 2019-02-25 08:46

Your Sleep method is ignoring the CancellationToken.

Try something like

public static CancellationTokenSource cancelSource ;

void Main()
{
    RetryAction(() => Sleep(), 500);
}

public static void RetryAction(Action action, int timeout)
{
     cancelSource = new CancellationTokenSource();                
     cancelSource.CancelAfter(timeout);

     Task.Run(() => action(), cancelSource.Token);    
}

public static void Sleep() 
{
    for(int i = 0 ; i< 50; i++)
    {
        "Waiting".Dump();
        System.Threading.Thread.Sleep(100);

        if (cancelSource.IsCancellationRequested)
        {
            "Cancelled".Dump();
            return;
        }
    }
    "done".Dump();
}
查看更多
登录 后发表回答