Reputation: 27
I've written a application and using 6 timers that must start after each other but these timers don't work properly. I don't know much about timers.
For example, timer1 start and something happen in application. then timer1 must stop forever and timer2 must start immediately and something happen in application. Then timer2 must stop forever and timer3 must start and so on.
Please help.
Here is my code:
int yyyy = 0;
void move()
{
yyyy++;
if (yyyy <= 1)
{
timer1.Start();
timer1.Interval = 15;
timer1.Tick += new EventHandler(timer_Tick1);
}
if (yyyy <= 2)
{
timer2.Start();
timer2.Interval = 15;
timer2.Tick += new EventHandler(timer_Tick2);
}
if (yyyy <= 3)
{
timer3.Start();
timer3.Interval = 15;
timer3.Tick += new EventHandler(timer_Tick3);
}
if (yyyy <= 4)
{
timer4.Start();
timer4.Interval = 15;
timer4.Tick += new EventHandler(timer_Tick4);
}
if (yyyy <= 5)
{
timer5.Start();
timer5.Interval = 15;
timer5.Tick += new EventHandler(timer_Tick5);
}
if (yyyy <= 6)
{
timer6.Start();
timer6.Interval = 15;
timer6.Tick += new EventHandler(timer_Tick6);
}
}
and: (for timer2 for example).
( all timers have exactly same below code).
int t = 0;
private void timer_Tick2(object sender, EventArgs e)
{
t++;
if (t <= 150)
{
// do somthing
}
else
timer2.Stop();
}
Upvotes: 3
Views: 383
Reputation: 48537
You need to put the timer.Start
calls in the Tick
method. e.g.
private void timer1_Tick(object sender, EventArgs e)
{
// Make sure you stop the timer first
timer1.Stop();
// Do something
timer1.Enabled = false;
timer2.Enabled = true;
timer2.Start();
}
This certainly isn't the way to go forward though. Implement a single Timer
and use an application state that you can check to see which method should be called next. This will reduce complexity and make your code a lot simpler. e.g.
private void timer1_Tick(object sender, EventArgs e)
{
// Stop the timer
timer1.Stop();
// Select which method should be called
switch (whatDoINeedToRun)
{
case "RunMeSecond":
// Do something
break;
case "RunMeThird":
// Do something
break;
case "RunMeFourth":
// Do something
break;
// etc.
default:
// This is the first method call
break;
}
// Restart the timer
timer1.Start();
}
Upvotes: 3
Reputation: 6278
Instead of creating 6 timers, why don't you just change the handler for one timer and start/stop it as desired? It does nto appear that your timers are at all running in parallel.
Upvotes: -1
Reputation: 60684
The first thing to consider here, is that if all 6 timers have the exact same code, I'm certain that your better of using only one timer, and instead keep a state that let's you know if you are in mode 1,2,3,4,5 or 6. This will also remove the need to stop the first timer, and start the new one.
So have a class variable called State that starts out as 1. When something happens in the application, set the state to 2, and let the original timer keep on running.
Having 6 code blocks that are identical will almost guarantee you that some time when you change the code, you will make a mistake in at least 1 of those 6 duplicates.
Upvotes: 1
Reputation: 1499770
It sounds like you don't need six timers - you need one timer, which does one of six actions when it fires, depending on the current state. I think that would lead to much simpler code than starting up multiple timers.
Upvotes: 2