当前位置:首页 > C#教程 > C#高级

C# 超时工具类 第二版

附源码,没有附测试demo

 

            ///
            <summary>
            ///
             超时工具
    
            ///
            </summary>
            public
            class
             TimeoutTools
    {
        
            private
             Timer timer;

        
            ///
            <summary>
            ///
             事件是否正在执行
        
            ///
            </summary>
            private
            bool EventRunning { get; set; }

        ///<summary>/// 位置
        ///</summary>privateuint Position { get; set; }



        ///<summary>/// 超时事情
        ///</summary>publicevent EventHandler TimeoutEvent;

        ///<summary>/// 步长值
        ///<para>默认值1</para>///</summary>publicuint StepLength { get; set; }

        ///<summary>/// 超时长度
        ///<para>默认180</para>///</summary>publicuint TimeoutLength { get; set; }


        ///<summary>/// 默认构造函数
        ///</summary>public TimeoutTools()
        {
            this.StepLength = 1;
            this.TimeoutLength = 180;

            this.timer = new Timer((obj) =>
            {
                this.Position += this.StepLength;

                if (this.Position >= this.TimeoutLength)
                {
                    this.Reset();

                    this.OnTimeOut();
                }
            });

            // 一秒一次this.SetTimerParam(0, 1000);
        }



        ///<summary>/// 指定超时时间 执行某个方法        
        ///</summary>///<returns>执行 是否超时</returns>publicstaticbool DoAction(TimeSpan timeSpan, Action action)
        {
            if (action == null)
                thrownew ArgumentNullException("action is null");

            bool timeout = true;

            try
            {
                // 异步调用Action
                IAsyncResult result = action.BeginInvoke(null, null);                
                
                // Waitif (result.AsyncWaitHandle.WaitOne(timeSpan, false))
                {
                    timeout = false;
                }

                if (!timeout)
                {
                    action.EndInvoke(result);
                }
            }
            catch (Exception)
            {
                timeout = true;
            }

            return timeout;
        }


        ///<summary>/// 设置计时器参数
        ///</summary>///<param name="dueTime">毫秒 默认值0</param>///<param name="period">毫秒 默认值1000</param>publicvoid SetTimerParam(int dueTime, int period)
        {
            this.timer.Change(dueTime, period);
        }


        ///<summary>/// 接收到信号
        ///</summary>publicvoid Reset()
        {
            this.Position = 0;
        }


        protectedvoid OnTimeOut()
        {
            if (this.EventRunning)
            {
                return;
            }

            this.EventRunning = true;

            if (this.TimeoutEvent != null)
            {
                this.TimeoutEvent(this, EventArgs.Empty);
            }

            this.EventRunning = false;
        }

    }
    

 

原文:http://www.cnblogs.com/08shiyan/p/4323196.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!