码迷,mamicode.com
首页 > 其他好文 > 详细

重试操作类

时间:2016-07-19 20:22:52      阅读:183      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!