最干净的写重试逻辑的方法?偶尔,在放弃之前,我需要重新尝试几次手术。我的代码是:int retries = 3;while(true) {
try {
DoSomething();
break; // success!
} catch {
if(--retries == 0) throw;
else Thread.Sleep(1000);
}}我想在一般重试函数中重写这个函数,例如:TryThreeTimes(DoSomething);有可能在C#吗?的代码是什么?TryThreeTimes()方法?
3 回答
TA贡献1789条经验 获得超8个赞
例
Policy .Handle<SqlException>(ex => ex.Number == 1205) .Or<ArgumentException>(ex => ex.ParamName == "example") .WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(3)) .Execute(() => DoSomething());
- 3 回答
- 0 关注
- 598 浏览
添加回答
举报
0/150
提交
取消
