Last active
December 18, 2015 02:49
-
-
Save gabrieljoelc/5713807 to your computer and use it in GitHub Desktop.
Generic retry utility ripped off from SO answer http://stackoverflow.com/a/1563234/34315.
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 class RetryUtility | |
{ | |
public static void RetryAction( Action action, int numRetries, int retryTimeout ) | |
{ | |
if( action == null ) | |
throw new ArgumentNullException("action"); // slightly safer... | |
do | |
{ | |
try { action(); return; } | |
catch | |
{ | |
if( numRetries <= 0 ) throw; // improved to avoid silent failure | |
else Thread.Sleep( retryTimeout ); | |
} | |
} while( numRetries-- > 0 ); | |
} | |
} |
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
RetryUtility.RetryAction( () => SomeFunctionThatCanFail(), 3, 1000 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment