Skip to content

Instantly share code, notes, and snippets.

@pykong
Created April 27, 2018 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pykong/bf9ef1beb42a726ab9568358a414dd3c to your computer and use it in GitHub Desktop.
Save pykong/bf9ef1beb42a726ab9568358a414dd3c to your computer and use it in GitHub Desktop.
public async Task DoWithRetryAsync(Func<Task> action, TimeSpan sleepPeriod, int tryCount = 3)
{
if (tryCount <= 0)
throw new ArgumentOutOfRangeException(nameof(tryCount));
while (true) {
try {
await action();
return; // success!
} catch {
if (--tryCount == 0)
throw;
await Task.Delay(sleepPeriod);
}
}
}
await DoWithRetryAsync(DoSomethingAsync, TimeSpan.FromSeconds(2), tryCount: 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment