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 ?