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;
}
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.
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With