0

I'm trying to make a simple countdown timer program. There are two timer objects. Once timer1 runs out of time, it stops and timer2 starts counting down. When timer2 runs out of time, timer1 starts again and so on. Here is my code:

private void timer1_Tick(object sender, EventArgs e)
    {
        milli1--;
        if(milli1 == -1)
        {
            sec1--;
            milli1 = 59;
            if (sec1 == -1)
            {
                min1--;
                sec1 = 59;

                if (min1 == -1)
                {

                    min1 = 0;
                    sec1 = 0;
                    milli1 = 0;
                    Console.WriteLine("Timer1 stops!");
                    timer1.Stop();
                    timer2.Start();
                }
            }
        }

        //updates displayed time
    }

However, when timer1 stops, timer2 doesn't seem to start. Somehow, timer1 continues ticking and continuously outputs "Timer1 Stops!" to console. How do I fix this?

EDIT: Here is my timer2_Tick():

private void timer2_Tick(object sender, EventArgs e)
    {
        milli2--;
        if (milli2 == -1)
        {
            sec2--;
            milli2 = 59;
            if (sec2 == -1)
            {
                min2--;
                sec2 = 59;

                if (min2 == -1)
                {
                    min2 = 0;
                    sec2 = 0;
                    milli2 = 0;
                    timer2.Stop();
                    timer1.Start();
                }
            }
        }
        //updates displayed time
    }

EDIT 2: Two timers with same interval is a trivial matter. My code also doesn't work when the timers have different intervals.

Donovan Keating
  • 1,715
  • 3
  • 17
  • 27
  • What's the interval and code for timer2? Maybe it stops immediately and starts timer1 again? Why can't you reuse timer1? Besides, it is horrible code to read and understand what you are trying to do. Consider removing some nesting and try to rethink your code. – Peter Bons Jan 22 '17 at 16:49
  • 2
    By the way: if milli1 means milliseconds, there will be 1000 in a second, not 59. – Peter Bons Jan 22 '17 at 16:49
  • Show your code that registers the tick events for both timers – John Koerner Jan 22 '17 at 16:51
  • timer2_Tick() is essentially the same with timer1_Tick(). Their intervals are both 100. PS. Oh, thanks for the correction on milliseconds – Donovan Keating Jan 22 '17 at 16:52
  • @JohnKoerner edited my post – Donovan Keating Jan 22 '17 at 16:55
  • What are the initial values for the variables? is there any other interaction with the timers somewhere else? – Peter Bons Jan 22 '17 at 16:57
  • 1
    If BOTH timers use the same `tick` interval...then why TWO timers? – JohnG Jan 22 '17 at 16:58
  • 1
    By the way, if both timers do the same, then why not use one? – Peter Bons Jan 22 '17 at 16:58
  • @PeterBons i set all values to 0 when program compiles. The users gets to choose the value of the second to count down from. And no, the variables are not touched anywhere else in the program except when displaying the time to the user. – Donovan Keating Jan 22 '17 at 17:02
  • Hmmm. I suppose I'll try my luck with 1 timer. Still want to know why 2 timers don't work though. – Donovan Keating Jan 22 '17 at 17:02
  • It can work with TWO however when both have the same interval it seems redundant. – JohnG Jan 22 '17 at 17:04
  • @JohnG Somehow my timer2 won't start. That's the problem. Try running my code to see – Donovan Keating Jan 22 '17 at 17:06
  • Do you recognize that this is duplicate code? Almost copy&paste? That violates the DRY principle. You should probably build a class that encapsulates the behavior and then have 2 instances of that class. – Thomas Weller Jan 22 '17 at 18:16

1 Answers1

0

I am not sure why you want to use a Timer for a console application as it is not event driven as a Windows.Forms.Timer is. You may be using a Threading timer but your code won’t work as it is using this timer. So below I created a windows form application and set the output type to the console and used the timers as you described and it works as expected. I do not think you can use a timer the way you are in a console application. Again this code makes little sense as ONE timer will do the same thing. If you must use a console application then you may want to check the following post: How do you add a timer to a C# console application

int milli1 = 0;
int milli2 = 0;
int sec1 = 0;
int min1 = 0;
int sec2 = 0;
int min2 = 0;

public Form1() {
  InitializeComponent();
  timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e) {
  Console.WriteLine("Timer1 tick!");
  milli1--;
  if (milli1 == -1) {
    sec1--;
    milli1 = 59;
    if (sec1 == -1) {
      min1--;
      sec1 = 59;

      if (min1 == -1) {

        min1 = 0;
        sec1 = 0;
        milli1 = 0;
        //Console.WriteLine("Timer1 stops!");
        timer1.Stop();
        timer2.Start();
      }
    }
  }
  //updates displayed time
}

private void timer2_Tick(object sender, EventArgs e) {
  Console.WriteLine("Timer2 tick!");
  milli2--;
  if (milli2 == -1) {
    sec2--;
    milli2 = 59;
    if (sec2 == -1) {
      min2--;
      sec2 = 59;

      if (min2 == -1) {
        min2 = 0;
        sec2 = 0;
        milli2 = 0;
        timer2.Stop();
        timer1.Start();
      }
    }
  }
  //updates displayed time
}
}
Community
  • 1
  • 1
JohnG
  • 9,259
  • 2
  • 20
  • 29