Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
666 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As stated by both BFree and James, there is no difference in StartStop 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...