我有尝试从网络服务信息类几声:停止方法执行
public TResult Try(Func<TResult> func, int maxRetries)
{
TResult returnValue = default(TResult);
int numTries = 0;
bool succeeded = false;
while (numTries < maxRetries)
{
try
{
returnValue = func();
succeeded = true;
}
catch (Exception ex)
{
Log(ex,numTries);
}
finally
{
numTries++;
}
if (succeeded)
{
return returnValue;
}
else
{
if (numTries == maxRetries)
{
//ask user what to do
}
}
}
现在,经过“如果(numTries == maxRetries)”用户将能够如果他想继续选择试图从Web服务获取数据或取消。 我想在用户取消时才打开新窗体,并停止执行调用上述方法的方法。我不能只返回空或新的对象,因为运行这个反弹的方法将继续工作,并且在很多情况下会导致问题。
所以基本上,这看起来是这样的:
someValue = retry.Try(() => webService.method(),maxRetries));
//if user canceled after app wasn't able to get data stop execution as already another form is opened
我当然可以检查的,如果返回值为空,如:
someValue = retry.Try(()=>webService.method(),maxRetries));
if (someValue == null)
return;
但是,这将意味着很多变化的代码和我想避免它,如果我可以从Try方法做到最好。