Using System.Windows.Forms.Timer.Start()/Stop() versus Enabled = true/false
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, versus using the Enabled property?
For example, if we wish to pause a timer while we do some processing, we could do:
myTimer.Stop();
// Do something interesting here.
myTimer.Start();
or, we could do:
myTimer.Enabled = false;
// Do something interesting here.
myTimer.Enabled = true;
If there is no significant difference, is there a consensus in the community about which option to choose?
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.
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...
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.
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 :)