Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlexanderBaggett/bee70c529ebca0a20a6c5aeb621a20d3 to your computer and use it in GitHub Desktop.
Save AlexanderBaggett/bee70c529ebca0a20a6c5aeb621a20d3 to your computer and use it in GitHub Desktop.
Retry Script C#
public static void Retry(
Action action,
int coolDownSeconds,
int maxAttempt)
{
var exceptions = new List<Exception>();
for (int attempted = 0; attempted < maxAttempt; attempted++)
{
try
{
if (attempted > 0)
{
Thread.Sleep(coolDownSeconds * 1000);
}
action.Invoke();
break;
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
if (exceptions.Count == maxAttempt)
{
throw new AggregateException(exceptions);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment