可能重复:
C#最简单的写入重试逻辑的方法?
我有一个功能包含服务器的Web服务调用,由于网络中的一些干扰,有时会失败(无法连接远程服务器错误).代码在try catch块中.我想在try块中重新运行Web服务调用,以便成功完成Web调用.
Alb*_*nbo 14
const int MaxRetries = 5;
for(int i = 0; i < MaxRetries; i++)
{
try
{
// do stuff
break; // jump out of for loop if everything succeeded
}
catch(Exception)
{
Thread.Sleep(100); // optional delay here
}
}
Run Code Online (Sandbox Code Playgroud)
xan*_*ndy 12
bool success = false;
int retry = 0;
while (!success && retry<3)
{
try{
// web service calls
success = true;
} catch(Exception) {
retry ++;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12108 次 |
| 最近记录: |