Fairly new to C# and timers, although I've managed to do some really fun stuff in C#, however I'm not getting the hang of Timers.
Form1.cs:
private int counter;
static System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
public void goTimer()
{
// Set Counter
counter = 60;
// If timer is already enabled, stop it.
if (timer1.Enabled)
{
timer1.Dispose();
//timer1.Stop() <- also tried
}
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000; // 1 second
timer1.Start(); // Timer exists
txtCountdown.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
if(counter == 0)
{
timer1.Stop();
}
txtCountdown.Text = counter.ToString();
}
So, what happens is that it seems to work as intended, until you start calling goTimer(); from e.g. a button press, then it will speed up the (int) counter as many times as you pressed it... And after a while the memory will be eaten up.
In this case the users will be able to do call the timer function, as it will remove some objects, clear some data and refresh the session, but also when the timer reaches 0.
Using Winforms, I did not add a timer in visual studio (it's only referenced here in Form1.cs).
How do I terminate all timers, and then restart at (int) counter?