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

后端 未结 6 1636
闹比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:07

    No they are eachothers equivalent.

    See Timer.Enabled and Timer.Start / Timer.Stop

    To add to your Question about the consensus, I would say its probably better practice to use the Start/Stop methods and its also better for readability I suppose.

    James.

    0 讨论(0)
  • 2020-12-01 16:15

    As stated by both BFree and James, there is no difference in Start\Stop versus Enabled with regards to functionality. However, the decision on which to use should be based on context and your own coding style guidelines. It depends on how you want a reader of your code to interpret what you've written.

    For example, if you want them to see what you're doing as starting an operation and stopping that operation, you probably want to use Start/Stop. However, if you want to give the impression that you are enabling the accessibility or functionality of a feature then using Enabled and true/false is a more natural fit.

    I don't think a consensus is required on just using one or the other, you really have to decide based on the needs of your code and its maintenance.

    0 讨论(0)
  • 2020-12-01 16:18

    From Microsoft's Documentation:

    Calling the Start method is the same as setting Enabled to true. Likewise, calling the Stop method is the same as setting Enabled to false.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.enabled.aspx

    So, I guess there's no difference...

    0 讨论(0)
  • 2020-12-01 16:21

    I don't use timer.Stop() and timer.Start(), because they are subs of timer.Enabled. If you want to set the timer to false at the beginning of the application (at loading) , you must used timer.Enabled = false, timer.Stop() won't work. This is why I use timer.Enabled = false/true.

    0 讨论(0)
  • 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();
    };
    
    0 讨论(0)
  • 2020-12-01 16:29

    Personally, I don't like setting properties to have too much consequence other than changing a value, so I tend to stick to the Start()/Stop() as it's clear(er) to me that when you are invoking a method, you are asking for something to happen.

    That said, I don't suppose there is a whole lot of ambiguity about what setting Enabled = true is going to do :)

    0 讨论(0)
提交回复
热议问题