C# timer stop after some number of ticks automatically

前端 未结 5 1850
梦谈多话
梦谈多话 2021-01-18 05:53

How to stop a timer after some numbers of ticks or after, let\'s say, 3-4 seconds?

So I start a timer and I want after 10 ticks or after 2-3 seconds to stop autom

相关标签:
5条回答
  • 2021-01-18 05:57

    When the timer's specified interval is reached (after 3 seconds), timer1_Tick() event handler will be called and you could stop the timer within 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;
    }
    
    0 讨论(0)
  • 2021-01-18 06:01

    Assuming you are using the System.Windows.Forms.Tick. You can keep track of a counter, and the time it lives like so. Its a nice way to use the Tag property of a timer. This makes it reusable for other timers and keeps your code generic, instead of using a globally defined int counter for each timer.

    this code is quiet generic as you can assign this event handler to manage the time it lives, and another event handler to handle the specific actions the timer was created for.

        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 讨论(0)
  • 2021-01-18 06:02

    You can keep a counter like

     int counter = 0;
    

    then in every tick you increment it. After your limit you can stop timer then. Do this in your tick event

     counter++;
     if(counter ==10)  //or whatever your limit is
       yourtimer.Stop();
    
    0 讨论(0)
  • 2021-01-18 06:09

    i generally talking because you didn't mention which timer, but they all have ticks... so:

    you'll need a counter in the class like

    int count;
    

    which you'll initialize in the start of your timer, and you'll need a dateTime like

    DateTime start;
    

    which you'll initialize in the start of your timer:

    start = DateTime.Now;
    

    and in your tick method you'll do:

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

    here is a full 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 讨论(0)
  • 2021-01-18 06:13

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

    tmrAutoStop.Tag = 0;
    

    Then, with every tick add one...

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

    and check if it reached your desired number:

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

    Use this same technique to alternate the timer associated 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 讨论(0)
提交回复
热议问题