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?
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 :)From Microsoft's Documentation:
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.enabled.aspx
So, I guess there's no difference...
As stated by both BFree and James, there is no difference in
Start
\
Stop
versusEnabled
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 usingEnabled
andtrue/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.
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.
I don't use
timer.Stop()
andtimer.Start()
, because they are subs oftimer.Enabled
. If you want to set the timer to false at the beginning of the application (at loading) , you must usedtimer.Enabled = false
,timer.Stop()
won't work. This is why I usetimer.Enabled = false/true
.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: