Reputation: 348
When I press button1 the value of tabatas goes from 8 to 1 immediately then the timer1_tick method runs through once correctly`.
int goSecondsLeft = 20;
int restSecondsLeft = 10;
int tabatas = 8;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (goSecondsLeft >= 0)
{
lbGoOrRest.Text = "Go!";
lbSecondsRemaining.Text = goSecondsLeft.ToString() + " seconds";
goSecondsLeft--;
}
else if (restSecondsLeft >= 0)
{
lbGoOrRest.Text = "Rest";
lbSecondsRemaining.Text = restSecondsLeft.ToString() + " seconds";
restSecondsLeft--;
}
else
{
timerGo.Stop();
}
}
private void button1_Click(object sender, EventArgs e)
{
while (tabatas > 0)
{
lbTabatasRemaining.Text = "Tabatas remaining" + tabatas.ToString();
timerGo.Start();
tabatas--;
}
}
I put a break point at the line while(tabataas >0) and kept steping in and each time the while loop executed the value of tabata went down one as expected, but when I run the program it goes to 1 immediatly while the timer1_tick method continues on running.
My working code below:
else
{
goSecondsLeft = 20;
restSecondsLeft = 10;
tabatas--;
lbTabatasRemaining.Text = "Tabatas remaining" + tabatas.ToString();
timerGo.Stop();
}
}
private void button1_Click(object sender, EventArgs e)
{
timerGo.Start();
}
Upvotes: 2
Views: 114
Reputation: 65166
Your while
loop has nothing in it to cause a delay, so it runs through pretty much instantaneously. Maybe you thought Timer.Start
blocks for some period of time, but it doesn't. It just starts the timer in the background and instantly returns.
Not sure what exactly you're trying to do, but if you want the tabatas
variable to tick down along the time, remove the loop and decrement the variable in the timer event handler instead.
Upvotes: 3