zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    LED自动同步时钟(LED电子时钟系统)京准电子
    电力北斗对时装置(北斗时钟同步系统)详细介绍
    京准时间同步系统(NTP网络同步时钟)概述
    京准:NTP时钟服务器(NTP时钟服务器)研究分析
    医院时钟系统(子母钟系统)设计组成架构
    GPS校时器(北斗授时设备)助力政务云建设
    三大免费开源的php语言cms系统 用好它们让你一天建好一个网站
    想精通正则表达式 这几个正则表达式学习资料及工具你必须有!
    零基础建站如何配置PHP运行环境 几种服务器环境配置的选择和方法
    php代码如何加域名授权?开源php项目如何保护版权 商业授权?
  • 原文地址:https://www.cnblogs.com/TBW-Superhero/p/10560437.html
Copyright © 2011-2022 走看看