Example #1
0
        public T Retry <T>(Func <T> func)
        {
            T   result = default(T);
            var num    = 0;

            do
            {
                try
                {
                    num++;
                    result = func();
                    break;
                }
                catch (Exception)
                {
                    if (num <= _retryNumber)
                    {
                        TimeSpan delayTimeSpan = TimeSpan.FromMilliseconds(RetryMath.CalculateRetryBackoffMilliseconds(num, _defaultBackOff, _minimumBackOff, _maxBackOff));
                        Thread.Sleep(delayTimeSpan);
                    }
                    else
                    {
                        throw;
                    }
                }
            } while (num <= _retryNumber);

            return(result);
        }
Example #2
0
        public async Task RetryTask(Func <Task> funcTask)
        {
            var num = 0;

            do
            {
                try
                {
                    num++;
                    await funcTask();

                    break;
                }
                catch (Exception exception)
                {
                    //Console.WriteLine(exception.ToString());
                    if (num <= _retryNumber)
                    {
                        TimeSpan delayTimeSpan = TimeSpan.FromMilliseconds(RetryMath.CalculateRetryBackoffMilliseconds(num, _defaultBackOff, _minimumBackOff, _maxBackOff));
                        await Task.Delay(delayTimeSpan);
                    }
                    else
                    {
                        throw;
                    }
                }
            } while  (num <= _retryNumber);
        }