This is a generic helper to help try some action until given condition is met.
Write retry logic for operations like web request or file operation in a more readable way rather than a try-catch nested in a loop. You can specify end conditions based on return value or exception, config the retry interval, maximum retry count and maximum retry time flexibly, and more.
RetryHelper.Instance.Try(() => TryDoSomething()).UntilNoException();
// Basic usage
RetryHelper.Instance.Try(() => TryGetSomething()).Until(result => result < 0.1);
// Get the result from the retried method
var resultSmallEnough = RetryHelper.Instance.Try(() => TryGetSomething()).Until(result => result < 0.1);
// Specify interval
RetryHelper.Instance.Try(() => TryGetSomething()).WithTryInterval(100).Until(result => result < 0.1);
// Specify max try count, will throw TimeoutException if exceeded
RetryHelper.Instance.Try(() => TryGetSomething()).WithMaxTryCount(20).Until(result => result < 0.1);
// Can also constrain the total try time
RetryHelper.Instance.Try(() => TryGetSomething()).WithTimeLimit(TimeSpan.FromSeconds(10)).Until(result => result < 0.1);
// Specify the extra success/fail/timeout action
RetryHelper.Instance.Try(() => TryGetSomething())
.WithMaxTryCount(20)
.OnSuccess(result => Trace.TraceInformation(string.Format("Get result {0}.", result)))
.OnFailure(result => Trace.TraceWarning(string.Format("Try failed. Got {0}", result)))
.OnTimeout(lastResult => Trace.TraceError("Did not get result under 0.1 in 20 times."))
.Until(result => result < 0.1);
// Also get the tried count.
RetryHelper.Instance.Try(() => TryGetSomething())
.WithMaxTryCount(20)
.OnSuccess((result, triedCount) => Trace.TraceInformation(string.Format("Get expected result {0} after {1} times.", result, triedCount)))
.OnFailure((lastResult, triedCount) => Trace.TraceWarning(string.Format("Try failed after {0} times. Got {1}", triedCount, lastResult)))
.OnTimeout((lastResult, triedCount) => Trace.TraceError(string.Format("Did not get result under 0.1 in {0} times.", triedCount)))
.Until(result => result < 0.1);
- OnSuccess: Executed after the condition is met.
- OnFailure: Executed after each failed attempt and before the next attempt.
- OnTimeout: Executed after all allowed attempts are failed.
// Retry on any (non-fatal) exception
RetryHelper.Instance.Try(() => TryDoSomething()).UntilNoException();
// Retry on specific exception
RetryHelper.Instance.Try(() => TryDoSomething()).UntilNoException<ApplicationException>();
// Lambda can be simplified in this case
RetryHelper.Instance.Try(TryDoSomething).UntilNoException();
RetryHelper.Instance.DefaultMaxTryCount = 3;
RetryHelper.Instance.DefaultMaxTryTime = TimeSpan.FromSeconds(10);
RetryHelper.Instance.DefaultTryInterval = TimeSpan.FromMilliseconds(100);
var retryHelper = new RetryHelper(new TraceSource("MyTraceSource"))
{
DefaultMaxTryCount = 10,
DefaultMaxTryTime = TimeSpan.FromSeconds(30),
DefaultTryInterval = TimeSpan.FromMilliseconds(500)
}