Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 倒计时类中的奇怪行为_C#_Countdowntimer - Fatal编程技术网

C# 倒计时类中的奇怪行为

C# 倒计时类中的奇怪行为,c#,countdowntimer,C#,Countdowntimer,为相当长的帖子和大量代码提前道歉 我的应用程序具有定时自动保存功能。用户要求我提供剩余时间的视觉指示器。我做了一些关于倒计时的研究,最后写了下面的课程: public class CountDownTimer { private Timer timer; private int remaining; /////////////////////////////////////////////////////////////////////

为相当长的帖子和大量代码提前道歉

我的应用程序具有定时自动保存功能。用户要求我提供剩余时间的视觉指示器。我做了一些关于倒计时的研究,最后写了下面的课程:

public class CountDownTimer
    {

        private Timer timer;
        private int remaining;

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Count down ticked delegate. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ///
        /// <param name="remaining">    The remaining. </param>
        /// <param name="maximum">      The maximum. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public delegate void CountDownTickedDelegate(int remaining, int maximum);

        /// <summary>   Event queue for all listeners interested in Ticked event. </summary>
        public event CountDownTickedDelegate Ticked;

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Count down percent delegate. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ///
        /// <param name="percent">  The percent. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public delegate void CountDownPercentDelegate(int percent);

        /// <summary>   Event queue for all listeners interested in Percent events. </summary>
        public event CountDownPercentDelegate Percent;

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Count down done delegate. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public delegate void CountDownDoneDelegate();

        /// <summary>   Event queue for all listeners interested in Done events. </summary>
        public event CountDownDoneDelegate Done;

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Gets or sets the maximum value to count down from </summary>
        ///
        /// <value> The maximum value. </value>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public int Maximum
        {
            get;
            set;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Gets or sets a value indicating whether the timer is Paused. </summary>
        ///
        /// <value> true if paused, false if not. </value>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public bool Paused
        {
            get;
            set;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Starts this CountDownTimer. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public void Start()
        {
            timer = new Timer {
                Interval = 1000
            };
            timer.Tick += onTimerTick;
            timer.Enabled = true;
            remaining = Maximum;
            Paused = false;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Stops this CountDownTimer. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public void Stop()
        {
            if (timer == null)
            {
                return;
            }

            Paused = true;
            timer.Enabled = false;
            timer = null;
            if (Percent != null)
            {
                Percent(100);
            }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Resets and restarts this CountDownTimer. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public void Reset()
        {
            Stop();
            Start();
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Handles the timer tick event. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ///
        /// <param name="sender">   Source of the event. </param>
        /// <param name="e">        Event information to send to registered event handlers. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        private void onTimerTick(object sender, EventArgs e)
        {
            if (remaining > 0)
            {
                if (Ticked != null)
                {
                    Ticked(remaining, Maximum);
                }

                if (Percent != null)
                {
                    int percent = remaining * 100 / Maximum;
                    Percent(percent);
                }

                if (!Paused)
                {
                    remaining--;
                }
                else
                {
                    remaining = Maximum;
                }
            }
            else
            {
                Stop();

                if (Done != null)
                {
                    Done();
                }
            }
        }
    }
公共类倒计时
{
私人定时器;
剩余的私有int;
////////////////////////////////////////////////////////////////////////////////////////////////////
///倒计时勾选代表。
///
///Jon,18/06/2012。
///
///剩下的。
///最大值。
////////////////////////////////////////////////////////////////////////////////////////////////////
公共委托无效倒计时票证数据(剩余整数,最大整数);
///所有对勾选事件感兴趣的侦听器的事件队列。
公共事件倒计时勾选Legate勾选;
////////////////////////////////////////////////////////////////////////////////////////////////////
///代表的百分比倒计时。
///
///Jon,18/06/2012。
///
///百分比。
////////////////////////////////////////////////////////////////////////////////////////////////////
公共委托无效倒计时百分比委托(整数百分比);
///对百分比事件感兴趣的所有侦听器的事件队列。
公共事件倒计时百分比代表百分比;
////////////////////////////////////////////////////////////////////////////////////////////////////
///完成倒数计时。
///
///Jon,18/06/2012。
////////////////////////////////////////////////////////////////////////////////////////////////////
公共委托void CountDownDoneDelegate();
///对已完成事件感兴趣的所有侦听器的事件队列。
公共活动倒计时完成公使活动完成;
////////////////////////////////////////////////////////////////////////////////////////////////////
///获取或设置要从中倒计时的最大值
///
///最大值。
////////////////////////////////////////////////////////////////////////////////////////////////////
公共整数最大值
{
得到;
设置
}
////////////////////////////////////////////////////////////////////////////////////////////////////
///获取或设置一个值,该值指示计时器是否暂停。
///
///如果暂停,则为true;如果未暂停,则为false。
////////////////////////////////////////////////////////////////////////////////////////////////////
公共广播暂停
{
得到;
设置
}
////////////////////////////////////////////////////////////////////////////////////////////////////
///启动这个倒计时。
///
///Jon,18/06/2012。
////////////////////////////////////////////////////////////////////////////////////////////////////
公开作废开始()
{
定时器=新定时器{
间隔=1000
};
timer.Tick+=onTimerTick;
timer.Enabled=true;
剩余=最大值;
暂停=错误;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
///停止这个倒计时。
///
///Jon,18/06/2012。
////////////////////////////////////////////////////////////////////////////////////////////////////
公共停车场()
{
如果(计时器==null)
{
返回;
}
暂停=真;
timer.Enabled=false;
定时器=空;
如果(百分比!=null)
{
百分之一百(100);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
///重置并重新启动此倒计时。
///
///Jon,18/06/2012。
////////////////////////////////////////////////////////////////////////////////////////////////////
公共无效重置()
{
停止();
Start();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
///处理计时器滴答声事件。
///
///Jon,18/06/2012。
///
///事件的来源。
///要发送到已注册事件处理程序的事件信息。
////////////////////////////////////////////////////////////////////////////////////////////////////
私有void onTimerTick(对象发送方,事件参数e)
{
如果(剩余>0)
{
如果(勾选!=null)
{
勾选(剩余,最大值);
}
如果(百分比!=null)
{
整数百分比=剩余*100/最大值;
百分比(百分比);
}
如果(!暂停)
{
剩余--;
}
其他的
{
剩余=最大值;
}
}
其他的
{
停止();
如果(完成!=null)
{
完成();
}
}
}
}
我用的是计时器,每次它“启动”时,我都会减少一个计数器。每次递减都会启动一个事件,以便我的表单可以直观地显示它。当计数器达到零时,另一个事件启动自动保存

还包括一些其他位,以允许重新保存autosave
private void onTimerTick(object sender, EventArgs e)
{
    try
    {
        timer.Stop();
        //Do your stuff here
    }
    finally { timer.Start(); }
}