SHARE
TWEET

Untitled

a guest Aug 18th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static class RetryHelper
  2. {
  3.     private static ILog logger = LogManager.GetLogger(); //use a logger or trace of your choice
  4.    
  5.     public static void RetryOnException(int times, TimeSpan delay,  Action operation)
  6.     {
  7.         var attempts = 0;
  8.         do
  9.         {
  10.             try
  11.             {
  12.                 attempts++;
  13.                 operation();
  14.                 break; // Sucess! Lets exit the loop!
  15.             }
  16.             catch (Exception ex)
  17.             {              
  18.                 if (attempts == times)
  19.                     throw;
  20.                
  21.                 logger.Error($"Exception caught on attempt {attempts} - will retry after delay {delay}", ex);  
  22.                              
  23.                 Task.Delay(delay).Wait();
  24.             }
  25.         } while (true);
  26.     }
  27. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top