Created
April 27, 2018 09:58
-
-
Save pykong/bf9ef1beb42a726ab9568358a414dd3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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