首页 > 其他 > 详细

重试操作类

时间:2016-07-19 20:22:45      阅读:152      评论:0      收藏:0      [点我收藏+]
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);
    }
}

重试操作类

原文:http://www.cnblogs.com/lenmom/p/5685886.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!