38 lines
846 B
C#
38 lines
846 B
C#
|
namespace DispenserCommon.Utils;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 重试工具类
|
|||
|
/// </summary>
|
|||
|
public class RetryHelper
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 进行重试
|
|||
|
/// </summary>
|
|||
|
/// <param name="action"></param>
|
|||
|
/// <param name="maxRetries"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static bool Retry(Action action, int maxRetries)
|
|||
|
{
|
|||
|
for (var attempt = 0; attempt < maxRetries; attempt++)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
action();
|
|||
|
// 成功后返回 true
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
if (attempt == maxRetries - 1)
|
|||
|
{
|
|||
|
// 超过重试次数返回 false
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
Thread.Sleep(200);
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|