C # timer stops after some ticks automatically

How to stop the timer after several ticks or after, say, 3-4 seconds?

So, I start the timer and want to stop automatically after 10 ticks or after 2-3 seconds.

Thanks!

+6
source share
5 answers

You can save the counter, for example

int counter = 0; 

then in every tick you increase it. After your restriction, you can stop the timer. Do it in your event tick

  counter++; if(counter ==10) //or whatever your limit is yourtimer.Stop(); 
+9
source

When the specified timer interval is reached (after 3 seconds), the timer1_Tick() event handler will be called, and you can stop the timer in the event handler.

 Timer timer1 = new Timer(); timer1.Interval = 3000; timer1.Enabled = true; timer1.Tick += new System.EventHandler(timer1_Tick); void timer1_Tick(object sender, EventArgs e) { timer1.Stop(); // or timer1.Enabled = false; } 
+4
source

Assuming you are using System.Windows.Forms.Tick . You can track the counter and the time when it lives like this. Its a good way to use the timer tag property. This makes it reusable for other timers and saves your common code, instead of using the globally defined int counter for each timer.

this code is quiet because you can designate a handler for this event to control its lifetime and another handler for events to handle certain actions for which a timer has been created.

  System.Windows.Forms.Timer ExampleTimer = new System.Windows.Forms.Timer(); ExampleTimer.Tag = new CustomTimerStruct { Counter = 0, StartDateTime = DateTime.Now, MaximumSecondsToLive = 10, MaximumTicksToLive = 4 }; //Note the order of assigning the handlers. As this is the order they are executed. ExampleTimer.Tick += Generic_Tick; ExampleTimer.Tick += Work_Tick; ExampleTimer.Interval = 1; ExampleTimer.Start(); public struct CustomTimerStruct { public uint Counter; public DateTime StartDateTime; public uint MaximumSecondsToLive; public uint MaximumTicksToLive; } void Generic_Tick(object sender, EventArgs e) { System.Windows.Forms.Timer thisTimer = sender as System.Windows.Forms.Timer; CustomTimerStruct TimerInfo = (CustomTimerStruct)thisTimer.Tag; TimerInfo.Counter++; //Stop the timer based on its number of ticks if (TimerInfo.Counter > TimerInfo.MaximumTicksToLive) thisTimer.Stop(); //Stops the timer based on the time its alive if (DateTime.Now.Subtract(TimerInfo.StartDateTime).TotalSeconds > TimerInfo.MaximumSecondsToLive) thisTimer.Stop(); } void Work_Tick(object sender, EventArgs e) { //Do work specifically for this timer } 
0
source

I generally say because you did not indicate which timer, but everyone has ticks ... like this:

you will need a counter in the class e.g.

 int count; 

which you initialize at the beginning of your timer and you will need a dateTime, for example

 DateTime start; 

which you initialize at the beginning of your timer:

 start = DateTime.Now; 

and in your selection method you will do:

 if(count++ == 10 || (DateTime.Now - start).TotalSeconds > 2) timer.stop() 

here is a complete example

 public partial class meClass : Form { private System.Windows.Forms.Timer t; private int count; private DateTime start; public meClass() { t = new Timer(); t.Interval = 50; t.Tick += new EventHandler(t_Tick); count = 0; start = DateTime.Now; t.Start(); } void t_Tick(object sender, EventArgs e) { if (count++ >= 10 || (DateTime.Now - start).TotalSeconds > 10) { t.Stop(); } // do your stuff } } 
0
source

When initializing your timer, set the tag value to 0 (zero).

 tmrAutoStop.Tag = 0; 

Then with each tick add one ...

 tmrAutoStop.Tag = int.Parse(tmrAutoStop.Tag.ToString()) + 1; 

and check if it has reached the desired number:

 if (int.Parse(tmrAutoStop.Tag.ToString()) >= 10) { //do timer cleanup } 

Use the same technique to alternate a timer-related event:

 if (int.Parse(tmrAutoStop.Tag.ToString()) % 2 == 0) { //do something... } else { //do something else... } 

To check elapsed time (in seconds):

 int m = int.Parse(tmrAutoStop.Tag.ToString()) * (1000 / tmrAutoStop.Interval); 
0
source

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


All Articles