Created
June 16, 2015 02:28
-
-
Save taylorc/579935800023abcd5969 to your computer and use it in GitHub Desktop.
Call Waiting
This file contains hidden or 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 CallWaiting(Action fn, int retryAttempts = 100, int threadSleepValue = 1000) | |
| { | |
| // We will try to call the function up to 100 times... | |
| int j = 0; | |
| for (int i = 0; i < retryAttempts; ++i) | |
| { | |
| j = i; | |
| try | |
| { | |
| // We call the function passed in and return the result... | |
| fn.Invoke(); | |
| break; | |
| } | |
| catch (Exception ex) | |
| { | |
| // We've caught a COM exception, which is most likely | |
| // a Server is Busy exception. So we sleep for a short | |
| // while, and then try again... | |
| clsUtils.LogMessageToFile(String.Concat(fn.Target.ToString(), " :", ex.Message)); | |
| Thread.Sleep(threadSleepValue); | |
| } | |
| } | |
| if (j + 1 == retryAttempts) | |
| { | |
| throw new Exception("'CallWaiting' failed to call function after " + (retryAttempts * threadSleepValue) / 1000 + " sec."); | |
| } | |
| } | |
| /// <summary> | |
| /// Wrap a method call that return T in a retry loop. Attemps 20 times every 100ms. | |
| /// </summary> | |
| /// <param name="fn">method to call</param> | |
| public static T CallWaiting<T>(Func<T> fn, int retryAttempts = 100, int threadSleepValue = 100) | |
| { | |
| // We will try to call the function up to 100 times... | |
| for (int i = 0; i < retryAttempts; ++i) | |
| { | |
| try | |
| { | |
| // We call the function passed in and return the result... | |
| return fn(); | |
| } | |
| catch (Exception ex) | |
| { | |
| // We've caught a COM exception, which is most likely | |
| // a Server is Busy exception. So we sleep for a short | |
| // while, and then try again... | |
| clsUtils.LogMessageToFile(String.Concat(fn.Target.ToString(), " :", ex.Message + " ")); | |
| if (ex.InnerException != null) { | |
| clsUtils.LogMessageToFile(String.Concat(fn.Target.ToString(), " INNER EXCEPTION:", ex.InnerException.Message)); | |
| } | |
| Thread.Sleep(threadSleepValue); | |
| } | |
| catch (IOException ex) | |
| { | |
| clsUtils.LogMessageToFile(String.Concat(fn.Target.ToString(), " :", ex.Message)); | |
| // We've caught a IO exception, which is most likely | |
| // a file Busy exception. So we sleep for a short | |
| // while, and then try again... | |
| Thread.Sleep(threadSleepValue); | |
| } | |
| } | |
| throw new Exception("'CallWaiting' failed to call function after " + (retryAttempts * threadSleepValue) / 1000 + " sec."); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment