C# timer stop after some number of ticks automatically

前端 未结 5 1854
梦谈多话
梦谈多话 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;
    }
    

提交回复
热议问题