Skip to content

Instantly share code, notes, and snippets.

@jamescrowley
Last active August 29, 2015 14:05
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 jamescrowley/3d17fcf5b2f59d9ed3c0 to your computer and use it in GitHub Desktop.
Save jamescrowley/3d17fcf5b2f59d9ed3c0 to your computer and use it in GitHub Desktop.
Async retry
public static class TaskExtensions
{
public static async Task RetryWhileException<T>(this Task transientOperation, int retryCount, TimeSpan retryDelay) where T:Exception
{
int currentRetry = 0;
while (true)
{
try
{
await transientOperation;
break;
}
catch (T)
{
currentRetry++;
if (currentRetry > retryCount)
{
throw;
}
}
await Task.Delay(retryDelay);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment