Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

timer

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?

like image 630
Stewart Avatar asked Jun 18 '09 14:06

Stewart


People also ask

What is timer enabled in C#?

If Enabled is set to true and AutoReset is set to false , the Timer raises the Elapsed event only once, the first time the interval elapses. If the interval is set after the Timer has started, the count is reset.


1 Answers

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.

like image 121
Jeff Yates Avatar answered Sep 21 '22 19:09

Jeff Yates