Last active
August 29, 2015 14:05
-
-
Save jamescrowley/3d17fcf5b2f59d9ed3c0 to your computer and use it in GitHub Desktop.
Async retry
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 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