Last active
June 6, 2016 06:36
-
-
Save Dalstroem/fbf1e6cb914ed6504b743d338099d726 to your computer and use it in GitHub Desktop.
C# 6 feature for retrying a task nth times.
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 static async Task WithRetry(Func<Task> action, int retryCount = 3) | |
{ | |
var retries = 0; | |
while(true) | |
{ | |
try | |
{ | |
await action().ConfigureAwait(false); | |
return; | |
} | |
catch when (retries++ <= retryCount) | |
{ | |
// Back off for a while. | |
await Task.Delay(1000).ConfigureAwait(false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: https://twitter.com/Nick_Craver/status/739420321686650880