How to check how much time is left before the timer fires the next event?

For example, I set timer1.Intervalto 5000, and I would like to know how much this interval remains until the timer mark. How can i do this?

+3
source share
5 answers

How to check timer time?

You can not. In timer classes, there is no way to check how long it remains before a timer due to fire. The best thing you can do is keep track of when the last timer starts, and calculate how much time is left until the next tick.

+4
source
        DateTime MainTimer_last_Tick = DateTime.Now;
        System.Windows.Forms.Timer timer_Calc_Remaining;
        timer_Calc_Remaining.Enabled = true;
        timer_Calc_Remaining.Interval = 100;
        timer_Calc_Remaining.Tick += new System.EventHandler(this.timer_Calc_Remaining_Tick);

at the start of the main timer or tick:

   timer_Main.Enabled = true;
        MainTimer_last_Tick = DateTime.Now;


 private void timer_Calc_Remaining_Tick(object sender, EventArgs e)
        {
            int remaining_Milliseconds = (int)(MainTimer_last_Tick.AddMilliseconds(timer_Main.Interval).Subtract(DateTime.Now).TotalMilliseconds);
.../*
        int newValue = (timer_Main.Interval -remaining_Milliseconds) ;


        progressBar1.Maximum = timer_Main.Interval+1;

        progressBar1.Value = newValue ;*/



    }
+2
source

1000 (). , 1 , .

Each time this timer expires, you can check the number of ticks (is it always a lot of 1000?) And subtract it from (or use modulo) 5000.

+1
source

But I can not:

This is because the sign is less than the sign before the equal sign, and not vice versa

Its <=not=<

if (timer1.Interval <= 5000)
{
    //do something
}
0
source

You are using Interval, which is a property that will not be changed (it will always be 5000, so checking if it is less than 5000 is pointless). Also, if this code is in a function Timer1_Tick, it will not be effective. However, the code I think you need is:

if (timer1.Interval <= 5000)
{
  //do something
}
0
source

All Articles