C#タイムアウトツールクラス第2版

12910 ワード

ソースコードを添付し、テストdemoを添付していません
 
以前のツールクラス:C#あるメソッドに実行タイムアウト時間を設定
 
    /// <summary>
    ///  
    /// </summary>
    public class TimeoutTools : IDisposable
    {
        private System.Windows.Forms.Timer timer;

        /// <summary>
        ///  
        /// </summary>
        public uint Position { get; private set; }

        /// <summary>
        ///  
        /// </summary>
        public event EventHandler TimeoutEvent;

        /// <summary>
        /// Tick 
        /// </summary>
        public event EventHandler TickEvent;

        /// <summary>
        ///  
        /// <para> 1</para>
        /// </summary>
        public uint StepLength { get; set; }

        /// <summary>
        ///  
        /// <para> 180</para>
        /// </summary>
        public uint TimeoutLength { get; set; }


        /// <summary>
        ///  
        /// </summary>
        public TimeoutTools(System.ComponentModel.IContainer container)
        {
            this.StepLength = 1;
            this.TimeoutLength = 180;

            this.timer = new System.Windows.Forms.Timer(container);
            this.timer.Interval = 1000;
            this.timer.Enabled = false;

            timer.Tick += (sender, e) =>
            {
                this.Position += this.StepLength;

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

                    this.OnTimeOut();
                }
                else
                {
                    if (this.TickEvent != null)
                    {
                        this.TickEvent(this, EventArgs.Empty);
                    }
                }
            };
        }


        ///<summary>        
        ///            
        ///</summary>
        ///<returns>   </returns>
        public static bool DoAction(TimeSpan timeSpan, Action action)
        {
            if (action == null)
                throw new ArgumentNullException("action is null");

            bool timeout = true;

            try
            {
                //  Action
                IAsyncResult result = action.BeginInvoke(null, null);

                // Wait
                if (result.AsyncWaitHandle.WaitOne(timeSpan, false))
                {
                    timeout = false;
                }

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

            return timeout;
        }


        /// <summary>
        ///  
        /// </summary>
        /// <param name="period"> </param>
        public void SetInterval(int period)
        {
            if (period == Timeout.Infinite)
            {
                this.timer.Enabled = false;
                return;
            }
            this.timer.Interval = period;
            this.timer.Enabled = true;
        }


        /// <summary>
        ///  
        /// </summary>
        public void Reset()
        {
            this.Position = 0;
        }


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


        public void Dispose()
        {
            if (this.timer == null)
                return;

            this.timer.Enabled = false;
            this.timer.Dispose();
        }
    }