c# 自定义公共类CallFunction-调用函数信息帮助类

    /// <summary>
    /// 调用函数信息
    /// </summary>
    public class CallFunction
    {
        /// <summary>
        /// 执行函数信息
        /// </summary>
        private readonly FunctionInfo _function = null;

        /// <summary>
        /// 重试总数
        /// </summary>
        private int retryCount;

        public CallFunction(FunctionInfo functionInfo)
        {
            if (functionInfo == null)
            {
                throw new Exception("functionInfo为null");
            }

            if (functionInfo.Func == null)
            {
                throw new Exception("functionInfo.Func为null");
            }
            _function = functionInfo;
            retryCount = functionInfo.RetryCount;
        }

        /// <summary>
        /// 执行 信息
        /// </summary>
        /// <returns></returns>
        public FunctionResult Invoke()
        {
            int timeOutCount = 0; //超时次数
            var lisExceptions = new List<RetryException>(); //异常集合
            FunctionResult functionResult = new FunctionResult(); //函数返回结果
            do
            {
                try
                {
                    AutoResetEvent autoResetEvent = new AutoResetEvent(false);
                    Thread thread = new Thread(() =>
                    {
                        try
                        {
                            functionResult.Result = _function.Func.Invoke();
                        }
                        catch (RetryException retryException) //重试异常,需要外面自定义
                        {
                            if (retryException.Exception != null)
                            {
                                functionResult.Exception = retryException.Exception;
                            }
                            functionResult.IsRetryException = true;
                        }
                        catch (Exception exception)
                        {
                            functionResult.Result = null;
                            functionResult.Exception = exception;
                        }
                        finally
                        {
                            try
                            {
                                autoResetEvent.Set();
                            }
                            catch
                            {
                                // ignored
                            }
                        }
                    }) { IsBackground = true, Priority = ThreadPriority.Highest };
                    thread.Start();
                    bool autoReset = autoResetEvent.WaitOne(TimeSpan.FromSeconds(_function.TimeOut)); //线程等
                    try
                    {
                        //thread.Abort();
                    }
                    catch
                    {
                        // ignored
                    }
                    try
                    {
                        autoResetEvent.Close();
                        autoResetEvent.Dispose();
                    }
                    catch
                    {
                        // ignored
                    }
                    if (functionResult.IsRetryException)
                    {
                        Thread.Sleep(1000); //执行失败在睡眠 1 毫秒
                        functionResult.IsRetryException = false;
                        throw new RetryException() { Exception = functionResult.Exception }; //重试异常
                    }
                    if (!autoReset) //
                    {
                        timeOutCount++; //超时次数
                        _function.RetryCount--;
                        Thread.Sleep(1000); //执行失败在睡眠 1 毫秒
                    }
                    else
                    {
                        return functionResult;
                    }
                }
                catch (RetryException retryException) //重试异常,需要外面自定义
                {
                    _function.RetryCount--;
                    lisExceptions.Add(retryException);
                }
                catch (Exception ex) //Exception 异常
                {
                    functionResult.Result = null;
                    functionResult.Exception = ex;
                    return functionResult;
                }
            } while (_function.RetryCount > 0);
            functionResult.Result = null;
            functionResult.Exception =
                new Exception("执行函数失败,超时次数:" + timeOutCount + "重试次数:" + (retryCount - _function.RetryCount),
                    lisExceptions.Count > 0 ? lisExceptions[lisExceptions.Count - 1].Exception : null);
            return functionResult;
        }




        ///// <summary>
        ///// 执行 信息
        ///// </summary>
        ///// <returns></returns>
        //public FunctionResult Invoke()
        //{
        //    int timeOutCount = 0; //超时次数
        //    var lisExceptions = new List<RetryException>(); //异常集合
        //    FunctionResult functionResult = new FunctionResult(); //函数返回结果
        //    do
        //    {
        //        try
        //        {
        //            IAsyncResult iAsyncResult = _function.Func.BeginInvoke(null, null);
        //            if (!iAsyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(_function.TimeOut))) //阻塞当前线程
        //            {
        //                try
        //                {
        //                    if (iAsyncResult.IsCompleted)
        //                    {
        //                        _function.Func.EndInvoke(iAsyncResult);
        //                    }
        //                    else
        //                    {
        //                        _function.Func.EndInvoke(iAsyncResult);
        //                    }
        //                }
        //                catch (Exception ex)
        //                {
        //                    iAsyncResult.AsyncWaitHandle.Close();
        //                    iAsyncResult.AsyncWaitHandle.Dispose();
        //                }
        //                timeOutCount++; //超时次数
        //                //超时重新连接
        //                _function.RetryCount--;
        //            }
        //            else
        //            {
        //                functionResult.Result = _function.Func.EndInvoke(iAsyncResult);
        //                return functionResult;
        //            }
        //        }
        //        catch (RetryException retryException) //重试异常,需要外面自定义
        //        {
        //            _function.RetryCount--;
        //            lisExceptions.Add(retryException);
        //        }
        //        catch (Exception ex) //Exception 异常
        //        {
        //            functionResult.Result = null;
        //            functionResult.Exception = ex;
        //            return functionResult;
        //        }
        //    } while (_function.RetryCount > 0);
        //    functionResult.Result = null;
        //    functionResult.Exception =
        //        new Exception("执行函数失败,超时次数:" + timeOutCount + "重试次数:" + (retryCount - _function.RetryCount),
        //            lisExceptions.Count > 0 ? lisExceptions[lisExceptions.Count - 1].Exception : null);
        //    return functionResult;
        //}
 
    }

    /// <summary>
    /// 函数对象信息
    /// </summary>
    public class FunctionInfo
    {
        private int _timeout = 20;
        /// <summary>
        /// 超时时间 以秒为单位,默认是20S
        /// </summary>
        public int TimeOut
        {
            get { return _timeout; }
            set { _timeout = value; }
        }
        /// <summary>
        /// 重试次数 默认 3次
        /// </summary>
        private int _retryCount = 3;

        /// <summary>
        /// 重试次数 默认 3次
        /// </summary>
        public int RetryCount
        {
            get { return _retryCount; }
            set { _retryCount = value; }
        }
        /// <summary>
        /// 没参数但可以返回的委托
        /// </summary>
        public Func<dynamic> Func { get; set; }
    }

    /// <summary>
    /// 函数执行结果
    /// </summary>
    public class FunctionResult
    {
        /// <summary>
        ///异常
        /// </summary>
        public Exception Exception { get; set; }
        /// <summary>
        /// 返回结果
        /// </summary>
        public dynamic Result
        {
            get;
            set;

        }
        /// <summary>
        /// 是否重试
        /// </summary>
        public bool IsRetryException { get; set; }
    }

调用方式:

 //可设置超时时间TimeOut(默认20s)及失败重试次数RetryCount(默认3次)
CallFunction cf = new CallFunction(new FunctionInfo() { Func = () => Test(1), RetryCount = 0, TimeOut = 5 }); FunctionResult r = cf.Invoke();

 

posted @ 2019-03-19 18:22  阿文sky  阅读(1237)  评论(0编辑  收藏  举报