Created
August 16, 2018 16:23
-
-
Save AlexanderBaggett/bee70c529ebca0a20a6c5aeb621a20d3 to your computer and use it in GitHub Desktop.
Retry Script C#
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 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