mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-04-25 22:29:47 +08:00
58 lines
1.4 KiB
C#
58 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GeniusInvokationAutoToy.Utils
|
|
{
|
|
/// <summary>
|
|
/// https://stackoverflow.com/questions/1563191/cleanest-way-to-write-retry-logic
|
|
/// </summary>
|
|
public static class Retry
|
|
{
|
|
public static void Do(
|
|
Action action,
|
|
TimeSpan retryInterval,
|
|
int maxAttemptCount = 3)
|
|
{
|
|
Do<object>(() =>
|
|
{
|
|
action();
|
|
return null;
|
|
}, retryInterval, maxAttemptCount);
|
|
}
|
|
|
|
public static T Do<T>(
|
|
Func<T> action,
|
|
TimeSpan retryInterval,
|
|
int maxAttemptCount = 3)
|
|
{
|
|
var exceptions = new List<Exception>();
|
|
|
|
for (int attempted = 0; attempted < maxAttemptCount; attempted++)
|
|
{
|
|
try
|
|
{
|
|
if (attempted > 0)
|
|
{
|
|
Thread.Sleep(retryInterval);
|
|
}
|
|
|
|
return action();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
exceptions.Add(ex);
|
|
}
|
|
}
|
|
|
|
if (exceptions.Count > 0)
|
|
{
|
|
throw exceptions.Last();
|
|
}
|
|
throw new AggregateException(exceptions);
|
|
}
|
|
}
|
|
} |