简体   繁体   English

如何倒计时直到计时器的下一个滴答?

[英]How to count down time until timer's next tick?

I have 2 timers. 我有2个计时器。 One of them is counting each time it ticks; 其中一个是每次滴答时计数; while using steady interval, or one randomly generated. 使用稳定间隔,或随机生成一个。 Second timer counts down to next tick in first timer. 第二个计时器倒计时到第一个计时器的下一个滴答

As of right now I am doing as such: 截至目前我正在这样做:

    private void btnStart_Click(object sender, EventArgs e)
    {
        nextClick = int.Parse(nudClickInterval.Value.ToString());

        if (nudPlusMinus.Value != 0) tmrClickInterval.Interval = random.Next( int.Parse(nudClickInterval.Value.ToString()) - int.Parse(nudPlusMinus.Value.ToString()), int.Parse(nudClickInterval.Value.ToString()) + int.Parse(nudPlusMinus.Value.ToString()));
        else tmrClickInterval.Interval = int.Parse(nudClickInterval.Value.ToString());

        tmrClickInterval.Start();
    }

    private void tmrClickInterval_Tick(object sender, EventArgs e)
    {
        if (nudPlusMinus.Value == 0) tmrClickInterval.Interval = int.Parse(nudClickInterval.Value.ToString());
        else tmrClickInterval.Interval = random.Next(int.Parse(nudClickInterval.Value.ToString()) - int.Parse(nudPlusMinus.Value.ToString()), int.Parse(nudClickInterval.Value.ToString()) + int.Parse(nudPlusMinus.Value.ToString()));

        tmrNextClick.Interval = tmrClickInterval.Interval / 10;
        tmrNextClick.Start();
        content++;
        nextClick = tmrClickInterval.Interval;
        label1.Text = content.ToString();
    }

    private void tmrNextClick_Tick(object sender, EventArgs e)
    {
        if (nextClick <= 0) tmrNextClick.Stop();
        else
        {
            nextClick = nextClick - (tmrClickInterval.Interval / 10);
            lblNextClickCount.Text = (nextClick / 100).ToString();
        }
    }

I am setting up an interval of my count down timer by using first timer's interval dividing by 10. The problem is that I keep getting some errors such as: Value '0' is not a valid value for Interval. Interval must be greater than 0. 我通过使用第一个定时器的间隔除以10来设置我的倒计时器的间隔。问题是我不断收到一些错误,例如: Value '0' is not a valid value for Interval. Interval must be greater than 0. Value '0' is not a valid value for Interval. Interval must be greater than 0. at line: tmrNextClick.Interval = tmrClickInterval.Interval / 10; Value '0' is not a valid value for Interval. Interval must be greater than 0.行: tmrNextClick.Interval = tmrClickInterval.Interval / 10; .

I'm not sure how to avoid my errors so I figure there might be a better way of counting down time until next timer tick. 我不知道如何避免我的错误,所以我认为可能有一种更好的方法来计算下一次计时器滴答时间。 Plus, I'd want a count down in nice steady interval instead but I am getting quite confused and not sure how to manage this problem. 另外,我希望在稳定的时间间隔内倒数,但我感到非常困惑,不知道如何解决这个问题。

Hope for some help. 希望得到一些帮助。

System.Windows.Forms.Timer has an int intervall. System.Windows.Forms.Timer有一个int intervall。 Dividing a number which is samller than 10 by 10 results in 0 (integer division!). 将一个小于10的数除以10得到0(整数除法!)。

Try to use System.Timers.Timer , it has an interval of type double , or check for 0 and assign 1 in that case. 尝试使用System.Timers.Timer ,它具有double类型的间隔,或检查0并在这种情况下分配1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM