简体   繁体   English

如何在下一个计时器滴答之前倒计时并显示时间?

[英]How to count down and display time before next timer tick?

I am trying to have one timer which will do things every 5 seconds or by user specified time interval. 我正在尝试使用一个计时器,该计时器每5秒或根据用户指定的时间间隔执行一次操作。

Now, I want to have a function which will count down in 10 milliseconds interval until tick of first timer. 现在,我想拥有一个功能,该功能将在10毫秒的间隔内倒计时,直到第一次计时器计时。 I have played around and found a simple way of doing this by doing count of 1/10 of first timers tick interval but when counting the number doesn't represent anything. 我玩耍了,找到了一种简单的方法,可以通过对第一次计时器的1/10间隔计数进行计数,但是在计数时并不代表任何东西。

How to do such count down? 这样倒数怎么办?

This is how I have at this time, but I want to change it: 这是我目前的情况,但是我想更改它:

    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()));

        if (tmrClickInterval.Interval / 10 == 0) tmrNextClick.Interval = 1;
        else 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();
        }
    }

First of all I am not sure of exactly what you are trying to do, also at 10 msec intervals you are pushing the minimum resolution of a timer. 首先,我不确定您要做什么,也不确定以10毫秒为间隔,您正在推动计时器的最低分辨率。 See this SO question . 看到这个问题 That being said you can try using the Diagnostics.Stopwatch Class to time the interval between your Tick events. 话虽如此,您可以尝试使用Diagnostics.Stopwatch类来计时Tick事件之间的时间间隔。 Something like this: 像这样:

private void tmrClickInterval_Tick(object sender, EventArgs e)
    {
        stopWatch.Stop();
        stopWatch.Reset();         
        stopWatch.Start();
        tmrNextClick.Start();
        content++;
        label1.Text = content.ToString();
    }

    private void tmrNextClick_Tick(object sender, EventArgs e)
    {
        nextClick = (((tmrClickInterval.Interval) - stopWatch.ElapsedMilliseconds) / 10) * 10;
        if (!(nextClick < 0))
        {
            lblNextClickCount.Text = nextClick.ToString();
        }
    } 

It seems like you are trying to count two intervals, a long interval and a short interval, whre the long interval is a multiple of the short interval. 似乎您正在尝试计算两个时间间隔,即长间隔和短间隔,而长间隔是短间隔的倍数。

If efficiency is not of the essence (its unlikely that it is) then I think I would have just a single timer that times the shorter interval. 如果效率不是最重要的(不太可能如此),那么我想我将只有一个计时器来对较短的间隔进行计时。 You know how many 'short ticks' equal a 'long tick' so just keep a count of how many short ticks there have been and when you have enough, fire your 'long tick event'. 您知道多少个“短滴答声”等于一个“长滴答声”,所以只需记下已有多少个短滴答声,当您有足够的时间时,触发“长滴答声事件”即可。

With two timers, you can get all sorts of race conditions. 使用两个计时器,您可以获得各种比赛条件。 Your 'short tick' might fire before the 'long tick' or it might happen the other way around. 您的“短勾”可能会在“长勾”之前触发,或者反之亦然。 The two timers may have jitter with respect to each other (although they'll be about right on average) so there's no telling which will fire first at any given time. 这两个计时器可能彼此之间有抖动(尽管它们在平均水平上大约是正确的),所以在任何给定时间都不会先触发哪个计时器。 With a single timer, you can control all of these factors. 使用单个计时器,您可以控制所有这些因素。

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

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