Skip to content

Instantly share code, notes, and snippets.

@Dalstroem
Last active June 6, 2016 06:36
Show Gist options
  • Save Dalstroem/fbf1e6cb914ed6504b743d338099d726 to your computer and use it in GitHub Desktop.
Save Dalstroem/fbf1e6cb914ed6504b743d338099d726 to your computer and use it in GitHub Desktop.
C# 6 feature for retrying a task nth times.
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);
}
}
}
@Dalstroem
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment