有时我需要在放弃之前重试几次操作。我的代码是这样的:
int retries = 3; while(true) { try { DoSomething(); break; // success! } catch { if(--retries == 0) throw; else Thread.Sleep(1000); } }
我想用一般的重试功能将其重写为:
TryThreeTimes(DoSomething);
在C#中可以吗?该TryThreeTimes()方法的代码是什么?
TryThreeTimes()
如果简单地重试相同的调用的全栈捕获语句用作通用异常处理机制,则可能很危险。话虽如此,这是一个基于lambda的重试包装器,您可以将其与任何方法一起使用。我选择将重试次数和重试超时因素作为参数,以提高灵活性:
public static class Retry { public static void Do( Action action, TimeSpan retryInterval, int maxAttemptCount = 3) { Do<object>(() => { action(); return null; }, retryInterval, maxAttemptCount); } public static T Do<T>( Func<T> action, TimeSpan retryInterval, int maxAttemptCount = 3) { var exceptions = new List<Exception>(); for (int attempted = 0; attempted < maxAttemptCount; attempted++) { try { if (attempted > 0) { Thread.Sleep(retryInterval); } return action(); } catch (Exception ex) { exceptions.Add(ex); } } throw new AggregateException(exceptions); } }
现在,您可以使用此实用程序方法执行重试逻辑:
Retry.Do(() => SomeFunctionThatCanFail(), TimeSpan.FromSeconds(1));
要么:
Retry.Do(SomeFunctionThatCanFail, TimeSpan.FromSeconds(1));
int result = Retry.Do(SomeFunctionWhichReturnsInt, TimeSpan.FromSeconds(1), 4);
否则,您甚至可以async超载。
async