Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class RetryHelper
- {
- private static ILog logger = LogManager.GetLogger(); //use a logger or trace of your choice
- public static void RetryOnException(int times, TimeSpan delay, Action operation)
- {
- var attempts = 0;
- do
- {
- try
- {
- attempts++;
- operation();
- break; // Sucess! Lets exit the loop!
- }
- catch (Exception ex)
- {
- if (attempts == times)
- throw;
- logger.Error($"Exception caught on attempt {attempts} - will retry after delay {delay}", ex);
- Task.Delay(delay).Wait();
- }
- } while (true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement