Occasionally I have a need to retry an operation several times before giving up. My code is like:
int retries = 3; while(true) { try { DoSomething();
Keep it simple with C# 6.0
public async Task Retry(Func action, TimeSpan retryInterval, int retryCount) { try { return action(); } catch when (retryCount != 0) { await Task.Delay(retryInterval); return await Retry(action, retryInterval, --retryCount); } }