使用System.Windows.Forms.Timer.Start()/停止()与启用=真/假Windows、System、Forms、Start

2023-09-03 00:04:02 作者:一腔孤勇

假设我们在.NET应用程序中使用System.Windows.Forms.Timer,有没有使用Start()和stop()上的计时器方法,与使用Enabled属性之间任何有意义的区别?

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;

如果没有区别显著,是存在于社会的共识哪些选项可供选择?

推荐答案

正如双方BFree和James,有在Start\Stop与Enabled至于功能。然而,在其上使用的决定,应根据上下文和自己的编码风格指南。这取决于你如何想你的code间preT你写什么的读者。

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.

例如,如果你想让他们看到你在做什么作为起始的操作,并停止该操作,你可能想使用启动/停止。不过,如果你想给IM pression将要启用功能的可访问性和功能,然后使用已启用真/假是一个更天作之合。

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.

我不认为一个共识,需要在只使用一个或另一个,你真的有一个基于你的code和维护的需求来决定。

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.