andrew4582

RetryHelper

May 16th, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4.  
  5. namespace Core {
  6.    
  7.     public static class RetryHelper {
  8.  
  9.         /// <summary>
  10.         /// Retries the specified function a certin amount of times before throwing the exception
  11.         /// </summary>
  12.         /// <typeparam name="T">Result of the sucessfull function</typeparam>
  13.         /// <param name="function">Function to excecute</param>
  14.         /// <param name="maxTries">The maximum amount of attempts to exceute the function</param>
  15.         /// <param name="timeoutMilliseconds">Timeout to wait for a function to finish execution</param>
  16.         /// <returns></returns>
  17.         public static T RetryOnFault<T>(Func<T> function,int maxTries,int timeoutMilliseconds = -1) {
  18.  
  19.             if(function == null)
  20.                 throw new ArgumentNullException("function");
  21.  
  22.             if(Math.Max(maxTries,0) == 0)
  23.                 throw new ArgumentOutOfRangeException("maxTries");
  24.  
  25.             for(int i = 0;i < maxTries;i++) {
  26.                 bool timedout = false;
  27.                 Exception error = null;
  28.                 T result = default(T);
  29.  
  30.                 AutoResetEvent waitEvent = new AutoResetEvent(false);
  31.  
  32.                 //queue up a worker thread
  33.                 ThreadPool.QueueUserWorkItem((state) =>
  34.                 {
  35.                     try {
  36.                         result = ((Func<T>)state)();
  37.                     }
  38.                     catch(Exception ferror) {
  39.                         error = ferror;
  40.                     }
  41.                     finally {
  42.                         //resume back
  43.                         if(!waitEvent.SafeWaitHandle.IsClosed)
  44.                             waitEvent.Set();
  45.                     }
  46.                 },function);
  47.  
  48.                 //wait on the task for the specified time
  49.                 timedout = !waitEvent.WaitOne(timeoutMilliseconds);
  50.                 waitEvent.Close();
  51.                 Debug.Write("Result: " + result);
  52.  
  53.                 //check if task faulted or timed out
  54.                 if(error != null || timedout) {
  55.                     if(timedout)
  56.                         error = new TimeoutException();
  57.  
  58.                     Debug.WriteLine("Attempt " + (i + 1) + " failed: " + error.Message);
  59.  
  60.                     //check how many errors
  61.                     if(i == maxTries - 1)
  62.                         throw error;
  63.                 }
  64.                 else
  65.                     return result;
  66.             }
  67.  
  68.             //should never get here
  69.             return default(T);
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment