Skip to content

Instantly share code, notes, and snippets.

@ctigeek ctigeek/Retry.cs
Created Apr 11, 2018

Embed
What would you like to do?
Retry pattern
public static class Retry
{
public static void Do(Action action, TimeSpan retryInterval, int maxAttemptCount)
{
try
{
action();
}
catch
{
if (maxAttemptCount == 1) throw;
Thread.Sleep(retryInterval);
Do(action, retryInterval, maxAttemptCount - 1);
}
}
public static T Do<T>(Func<T> func, TimeSpan retryInterval, int maxAttemptCount)
{
try
{
return func();
}
catch
{
if (maxAttemptCount == 1) throw;
Thread.Sleep(retryInterval);
return Do(func, retryInterval, maxAttemptCount - 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.