Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer Interval 1000 != 1 second?

Tags:

c#

timer

seconds

I have a label which should show the seconds of my timer (or in other word I have a variable to which is added 1 every interval of the timer). The interval of my timer is set to 1000, so the label should update itself every second (and should also show the seconds). But the label is after 1 second already in the hundreds. What is a proper interval to get 1 second?

int _counter = 0;
Timer timer;

timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(TimerEventProcessor);
label1.Text = _counter.ToString();
timer.Start();

private void TimerEventProcessor(object sender, EventArgs e)
{
  label1.Text = _counter.ToString();
  _counter += 1;
}
like image 854
Tim Kathete Stadler Avatar asked Jan 18 '13 09:01

Tim Kathete Stadler


People also ask

What is timer interval in C#?

The Timer class in C# represents a Timer control that executes a code block at a specified interval of time repeatedly. For example, backing up a folder every 10 minutes, or writing to a log file every second. The method that needs to be executed is placed inside the event of the timer.

What is timer control?

The timer control is a looping control used to repeat any task in a given time interval. It is an important control used in Client-side and Server-side programming, also in Windows Services. Furthermore, if we want to execute an application after a specific amount of time, we can use the Timer Control.

What is timer interval in VB net?

The default interval is 100 milliseconds, but the range of possible values is 1 to 2,147,483,647. A timer is typically used in a VB.Net program by creating an event handler for its Tick event (the event handler contains code that will be executed when a Tick event occurs).

Which property should we have to fire timer event at defined time interval?

This means that the Elapsed event will fire at an interval defined by the resolution of the system clock if the Interval property is less than the resolution of the system clock. The following example sets the Interval property to 5 milliseconds.


1 Answers

The proper interval to get one second is 1000. The Interval property is the time between ticks in milliseconds:

MSDN: Timer.Interval Property

So, it's not the interval that you set that is wrong. Check the rest of your code for something like changing the interval of the timer, or binding the Tick event multiple times.

like image 110
Guffa Avatar answered Sep 30 '22 19:09

Guffa