Skip to content

Instantly share code, notes, and snippets.

@ctigeek
Created April 11, 2018 00:34
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 ctigeek/d9ac9c046e8c930d1acf46974d2b39ca to your computer and use it in GitHub Desktop.
Save ctigeek/d9ac9c046e8c930d1acf46974d2b39ca to your computer and use it in GitHub Desktop.
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