mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-22 03:02:35 +01:00
Add retry helper method
This commit is contained in:
parent
cec6a489d3
commit
bd69e6413a
40
src/libse/Common/Retry.cs
Normal file
40
src/libse/Common/Retry.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace Nikse.SubtitleEdit.Core.Common
|
||||
{
|
||||
public class Retry
|
||||
{
|
||||
public static void Do(Action action, TimeSpan retryInterval, int maxAttemptCount)
|
||||
{
|
||||
Do<object>(() =>
|
||||
{
|
||||
action();
|
||||
return null;
|
||||
}, retryInterval, maxAttemptCount);
|
||||
}
|
||||
|
||||
public static T Do<T>(Func<T> action, TimeSpan retryInterval, int maxAttemptCount)
|
||||
{
|
||||
var lastException = new Exception();
|
||||
for (int attempted = 0; attempted < maxAttemptCount; attempted++)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (attempted > 0)
|
||||
{
|
||||
Thread.Sleep(retryInterval);
|
||||
}
|
||||
return action();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
lastException = ex;
|
||||
}
|
||||
}
|
||||
|
||||
throw lastException;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user