using System; using System.Collections.Generic; using System.Threading; namespace JD.Core { public static class Retry { public static void Do(TimeSpan retryInterval, int retryCount, Action action) { Do(retryInterval, retryCount, () => { action(); return null; }); } public static T Do(TimeSpan retryInterval, int retryCount, Func action) { var exceptions = new List(); for (var retry = 0; retry < retryCount; retry++) { try { return action(); } catch (Exception ex) { exceptions.Add(ex); Thread.Sleep(retryInterval); } } throw new AggregateException(exceptions); } } }