gpt4 book ai didi

c# - C#如何实现异常重试n次?

转载 作者:太空狗 更新时间:2023-10-29 18:25:49 26 4
gpt4 key购买 nike

我正在开发一个 WPF 4.0 应用程序,我们在其中从远程 Web 服务获取数据。 Web 服务向其客户端公开了大约 120 多个方法。如果来 self 的 WPF 应用程序的 Web 服务调用失败,我需要重试 n 次,这可以通过 App.Config 进行配置。如何实现?有没有解决这个问题的设计模式?

最佳答案

static T TryNTimes<T>(Func<T> func, int times)
{
while (times>0)
{
try
{
return func();
}
catch(Exception e)
{
if (--times <= 0)
throw;
}

}
}

关于c# - C#如何实现异常重试n次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4853010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com