3

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();

    }
4
  • what problem are you having.. do you want that other timers dont start before previous timers ticks 150 times.. then you dont need timers at all.. it looks like plain sequential process Commented Feb 4, 2011 at 9:35
  • 2
    "these timers don't work properly" : Please describe. Commented Feb 4, 2011 at 9:35
  • Where are you incrementing yyyy? where are you re-calling move? and finally, why use 6 different timers, you can use a single timer and handle the different code blocks in a single callback using a switch? Commented Feb 4, 2011 at 9:37
  • 2
    You do realize that if yyyy is (for example) 1 then all of those timers will start, right? Commented Feb 4, 2011 at 9:37

4 Answers 4

3

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();
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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.

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.