简体   繁体   中英

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.

Now, I want to have a function which will count down in 10 milliseconds interval until tick of first timer. 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.

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. See this SO question . That being said you can try using the Diagnostics.Stopwatch Class to time the interval between your Tick events. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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