Method wait.retry(+ 1 overload)
Overload
Calls callback function action. If it throws an exception, waits/retries until it does not throw exceptions or until timeout.
public static bool retry(Seconds timeout, Action action, Func<Exception, bool> catchWhen = null)
Parameters
timeout (Seconds)
Timeout, seconds. Can be 0 (infinite), >0 (exception) or <0 (no exception). More info: Wait timeout. |
action (Action)
Callback function (eg lambda). It is called repeatedly, until does not throw an exception. Default period is 10, and can be changed like |
catchWhen (Func<Exception, bool>)
Called on exception. Return |
Returns
bool
Returns |
Exceptions
Examples
This example uses both wait.retry overloads - with parameter Func func
and with parameter Action action
.
string file = @"C:\Test\test.txt";
string s = wait.retry(5, () => File.ReadAllText(file));
s = s.Upper();
wait.retry(5, () => { File.WriteAllText(file, s); });
print.it("ok");
Set the retry period.
wait.retry(new(5) { Period = 100, MaxPeriod = 1000 }, () => { File.WriteAllText(file, s); });
Handle only exceptions of some types.
wait.retry(5, () => { File.WriteAllText(file, s); }, e => e is IOException);
Overload(top)
Calls callback function func and returns its result. If it throws an exception, waits/retries until it does not throw exceptions or until timeout.
public static T retry<T>(Seconds timeout, Func<T> func, Func<Exception, bool> catchWhen = null)
Parameters
timeout (Seconds)
Timeout, seconds. Can be 0 (infinite), >0 (exception) or <0 (no exception). More info: Wait timeout. |
func (Func<T>)
Callback function (eg lambda). It is called repeatedly, until does not throw an exception. Default period is 10, and can be changed like |
catchWhen (Func<Exception, bool>)
Called on exception. Return |
Returns
T
Returns the value returned by the callback function. On timeout returns |
Exceptions
Type Parameters
T |