lenmom

博客园 首页 新随笔 联系 订阅 管理
public class Retry
{
    // Methods
    public static void Do(Action action, int timeoutMS = 50, int retryCount = 3)
    {
        Do<object>(delegate {
            action();
            return null;
        }, timeoutMS, retryCount);
    }

    public static ResultType Do<ResultType>(Func<ResultType> action, int timeoutMS = 50, int retryCount = 3)
    {
        List<Exception> innerExceptions = new List<Exception>();
        for (int i = 0; i < retryCount; i++)
        {
            try
            {
                return action();
            }
            catch (Exception exception)
            {
                innerExceptions.Add(exception);
                Thread.Sleep(timeoutMS);
            }
        }
        throw new AggregateException(innerExceptions);
    }
}
posted on 2016-07-19 18:54  老董  阅读(155)  评论(0)    收藏  举报