I followed an example from Head First C# on DispatcherTimer.
First time I press the button the ticker will increase by 1 second, but the next time I click on the button the ticker will increase by 2 seconds for every second/tick. Third time ticker increases with 3 seconds and so on (1 second is added for every button press).
Why is that and how to i "reset" the ticker Interval so it will only increase by 1 second every time?
Here is code:
DispatcherTimer timer = new DispatcherTimer();
private void Button_Click_1(object sender, RoutedEventArgs e)
{
timer.Tick += timer_Tick;
timer.Interval = TimeSpan.FromMilliseconds(1000);
timer.Start();
CheckHappiness();
}
int i = 0;
void timer_Tick(object sender, object e)
{
ticker.Text = "Tick #" + i++;
}
private async void CheckHappiness()
{
... code ..
timer.Stop();
}
}
}
Cheers!
This line adds the method to the eventhandler everytime you press the button; in which you do an i++ which increases i by one. When you have two methods doing that at the same time (since the timer ticks on your interval) then you get an increase by two every tick of the timer.