Skip to content

Instantly share code, notes, and snippets.

@gabrieljoelc
Last active December 18, 2015 02:49
Show Gist options
  • Save gabrieljoelc/5713807 to your computer and use it in GitHub Desktop.
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.
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 );
}
}
RetryUtility.RetryAction( () => SomeFunctionThatCanFail(), 3, 1000 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment