tags:

views:

1335

answers:

5

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?

+3  A: 

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.

James
+2  A: 

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...

BFree
+5  A: 

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.

Jeff Yates
+1  A: 

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 :)

Jon Grant
A: 

Does both of them reset the timer? Is it possible that one of them pauses the timer and the other stops it, so next time it's started it's either continuing from the same time elapsed or from the zero point (Interval) ?

Think of a Chess game where every player pauses their timer once they made their move and then continue from the same point once their opponent made their move, so the timer shows the total time for each player in the game, as appose to a timer that limits a move and needs to be reset after previous move every time.

does timer.start() reset the timer? does timer.enabled=true?

Elad.

Elad