I'm programming in c#(WPF). I'm trying to call a function repetitively. I System.Timers
. When I try high values for timer such as 1 second = (1000ms), it works good but when I try low values it cant work.
To show what the problem is I used a code as you see below:
Timer stopWatchTimer = new Timer();
int timerCounter = 0;
// this method called in button
private void StartStopWatch()
{
stopWatchTimer.Interval = 1;
stopWatchTimer.Elapsed += new ElapsedEventHandler(stopWatchTimerElapsed);
stopWatchTimer.Enabled = true;
}
private void stopWatchTimerElapsed(object sender, ElapsedEventArgs e)
{
timerCounter++;
Action a = () =>
{
// this is a Label
lblCounter.Content = timerCounter;
};
lblCounter.Dispatcher.Invoke(a);
if(timerCounter == 200)
{
stopWatchTimer.Enabled = false;
}
}
for example I use a Label and set my interval to 200 ms. So my timer should be stopped less than 1 second (1/5 second) but it takes too long. where is the problem?