Using System.Windows.Forms.Timer.Start()/Stop() versus Enabled = true/false

后端 未结 6 1660
闹比i
闹比i 2020-12-01 15:37

Suppose we are using System.Windows.Forms.Timer in a .Net application, Is there any meaningful difference between using the Start() and Stop() methods on the timer,

6条回答
  •  孤城傲影
    2020-12-01 16:27

    Here's a simple code to test how Enabled, Start(), Stop() work with each other.

    Make a test Windows form app, add two simple buttons and paste this code inside Form1() constructor:

    int c = 0;
    Timer tmr1 = new Timer()
    {
        Interval = 100,
        Enabled= false
    };
    tmr1.Tick += delegate
    {
        c++;
    };
    
    // used to continously monitor the values of "c" and tmr1.Enabled
    Timer tmr2 = new Timer()
    {
        Interval = 100,
        Enabled = true
    };
    tmr2.Tick += delegate
    {
        this.Text = string.Format("c={0}, tmr1.Enabled={1}", c, tmr1.Enabled.ToString());
    };
    
    button1.Click += delegate
    {
        tmr1.Start();
    };
    button2.Click += delegate
    {
        tmr1.Stop();
    };
    

提交回复
热议问题