Пример #1
0
        /// <summary>
        /// invoke action, when fails call errorNotify to notify users if it is not null
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="action"></param>
        /// <param name="obj"></param>
        /// <param name="exceptionHandler">错误处理,最终出现错误时会通知</param>
        public void Invoke <T>(Action <T> action, T obj, Action <RetryException> exceptionHandler = null)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action is null");
            }

            int            tryCount       = 0;
            RetryException retryException = null;

            while (true)
            {
                try
                {
                    action(obj);
                    //一定要return,否则死循环
                    return;
                }
                catch (Exception ex)
                {
                    tryCount++;

                    #region 异常处理

                    if (retryException == null)
                    {
                        retryException = new RetryException();
                    }
                    retryException.Enqueue(ex);

                    if (tryCount >= TryCount)
                    {
                        if (exceptionHandler != null)
                        {
                            exceptionHandler(retryException);
                        }

                        return;
                    }
                    #endregion


                    if (TryInterval != null)
                    {
                        try
                        {
                            Thread.Sleep(TryInterval.Value);
                        }
                        catch (Exception)
                        {
                            //ignore
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// invoke func, when fails call errorNotify to notify users if it is not null
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="func"></param>
        /// <param name="obj"></param>
        /// <param name="exceptionHandler">错误处理,最终出现错误时会通知</param>
        /// <returns></returns>
        public TResult Invoke <T, TResult>(Func <T, TResult> func, T obj, Action <RetryException> exceptionHandler = null)
        {
            if (func == null)
            {
                throw new ArgumentNullException("action is null");
            }

            int            tryCount       = 0;
            RetryException retryException = null;

            while (true)
            {
                try
                {
                    return(func(obj));
                }
                catch (Exception ex)
                {
                    tryCount++;

                    #region 异常处理

                    if (retryException == null)
                    {
                        retryException = new RetryException();
                    }
                    retryException.Enqueue(ex);

                    if (exceptionHandler != null && tryCount >= TryCount)
                    {
                        exceptionHandler(retryException);
                    }
                    #endregion

                    if (tryCount >= TryCount)
                    {
                        return(default(TResult));
                    }

                    if (TryInterval != null)
                    {
                        try
                        {
                            Thread.Sleep(TryInterval.Value);
                        }
                        catch (Exception)
                        {
                            //ignore
                        }
                    }
                }
            }
        }