c.Retry

PHOTO EMBED

Wed Sep 22 2021 14:40:24 GMT+0000 (Coordinated Universal Time)

Saved by @rick_m #c#

public static class Retry
    {
        public static void Do(
            Action action,
            TimeSpan retryInterval,
            int retryCount = 3)
        {
            Do<object>(() =>
            {
                action();
                return null;
            }, retryInterval, retryCount);
        }

        public static T Do<T>(
            Func<T> action,
            TimeSpan retryInterval,
            int retryCount = 3)
        {
            var exceptions = new List<Exception>();

            for (int retry = 0; retry < retryCount; retry++)
            {
                try
                {
                    if (retry > 0)
                        Thread.Sleep(retryInterval);
                    return action();
                }
                catch (Exception ex)
                {
                    exceptions.Add(ex);
                }
            }

            throw new AggregateException(exceptions);
        }
    }
content_copyCOPY

DataTable results = Retry.Do(() => SqlDbClient.RunQuery(query, sourceConnection, parameters), TimeSpan.FromSeconds(retryDelayInSeconds));