Skip to content

Instantly share code, notes, and snippets.

@gekowa
Created January 7, 2016 07:07
Show Gist options
  • Save gekowa/984a71ff07a5e2824f2c to your computer and use it in GitHub Desktop.
Save gekowa/984a71ff07a5e2824f2c to your computer and use it in GitHub Desktop.
public static class Retry {
public static void Do(
Action action,
TimeSpan retryInterval,
int retryCount = 3) {
Do<object>(() => {
action();
return null;
}, retryInterval, retryCount);
}
public static T Do<T>(
Func<T> action,
TimeSpan retryInterval,
int retryCount = 3) {
var exceptions = new List<Exception>();
for (int retry = 0; retry < retryCount; retry++) {
try {
if (retry > 0)
Thread.Sleep(retryInterval);
return action();
} catch (Exception ex) {
exceptions.Add(ex);
}
}
throw new AggregateException(exceptions);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment