12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using Aitex.Core.RT.Event;
- namespace MECF.Framework.Common.Utilities
- {
-
-
-
- public class RetryInstance
- {
- public static RetryInstance Instance()
- {
- return new RetryInstance();
- }
-
-
-
-
-
-
-
-
-
-
-
- public TResult Execute<TResult>(
- Func<TResult> action,
- int secondsInterval,
- int retryCount,
- TResult expectedResult,
- bool isSuppressException = true
- )
- {
- var result = default(TResult);
- var exceptions = new List<Exception>();
- if (retryCount == 0)
- {
- retryCount = Int32.MaxValue;
- }
- for (var retry = 0; retry < retryCount; retry++)
- {
- try
- {
- if (retry > 0) Thread.Sleep(secondsInterval * 1000);
- EV.PostInfoLog(action.ToString(),$"Retry Instance executing {retry+1} times, result : {result}");
- result = action();
- }
- catch (Exception ex)
- {
- exceptions.Add(ex);
- }
- if (result.Equals(expectedResult)) return result;
- }
- if (!isSuppressException)
- throw new AggregateException(exceptions);
- else
- return result;
- }
- }
- }
|