Problem with timers in C #

I wrote an application and I use 6 timers that should start one after another, but these timers do not work correctly. I don't know much about timers.

For example, the start of timer1 and something happens in the application. then timer1 should stop forever, and timer2 should start immediately, and something happens in the application. Then timer2 should stop forever, and timer3 should start, etc.

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 example, for timer2).

(all timers have exactly the same code below).

  int t = 0; private void timer_Tick2(object sender, EventArgs e) { t++; if (t <= 150) { // do somthing } else timer2.Stop(); } 
+4
source share
4 answers

You need to put the timer.Start calls in the Tick method. eg.

 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, of course, is not a way forward. Deploy a single Timer and use the state of the application, which you can check to find out which method should be called next. This will reduce complexity and make your code a lot easier. eg.

 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(); } 
+4
source

It seems that you do not need six timers - you need one timer that performs one of six actions when it fires, depending on the current state. I think this will lead to much simpler code than starting multiple timers.

+2
source

The first thing to consider here is that if all 6 timers have the same code, Iโ€™m sure you better use only one timer and instead save a state that will tell you if you are in mode 1, 2,3,4,5 or 6. This will also eliminate the need to stop the first timer and start a new one.

So, I have a State class variable that starts with 1. When something happens in the application, set the state to 2 and let the original timer continue to work.

With 6 identical blocks of code, you almost guarantee that for a while, when you change the code, you will make a mistake in at least one of these 6 duplicates.

+1
source

Instead of creating 6 timers, why don't you just change the handler for one timer and start / stop it? It doesn't seem that your timers generally work in parallel.

-1
source

Source: https://habr.com/ru/post/1338396/


All Articles