C# 使用 System.Windows.Forms.Timer.Start()/Stop() 与 Enabled = true/false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1012948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:32:06  来源:igfitidea点击:

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

c#.nettimer

提问by Stewart

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?

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

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?

如果没有显着差异,社区是否就选择哪个选项达成共识?

采纳答案by Jeff Yates

As stated by both BFreeand James, there is no difference in Start\Stopversus Enabledwith 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.

如通过陈述贝丽詹姆斯,也无明显差异对比与问候功能。但是,应该根据上下文和您自己的编码风格指南来决定使用哪个。这取决于您希望代码的读者如何解释您编写的内容。Start\StopEnabled

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 Enabledand true/falseis a more natural fit.

例如,如果您希望他们看到您在开始操作和停止操作时正在做什么,您可能想要使用Start/Stop. 但是,如果您想给人的印象是您正在启用某个功能的可访问性或功能,那么使用Enabledtrue/false更自然。

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.

我认为不需要就只使用一种或另一种达成共识,您确实必须根据代码及其维护的需要做出决定。

回答by James

No they are eachothers equivalent.

不,他们是彼此等价的。

See Timer.Enabledand Timer.Start/ Timer.Stop

请参阅Timer.EnabledTimer.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.

为了补充您关于共识的问题,我想说使用 Start/Stop 方法可能更好的做法,而且我认为它的可读性也更好。

James.

詹姆士。

回答by BFree

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.

调用 Start 方法与将 Enabled 设置为 true 相同。同样,调用 Stop 方法与将 Enabled 设置为 false 相同。

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.enabled.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.enabled.aspx

So, I guess there's no difference...

所以,我想没有什么区别......

回答by Jon Grant

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.

就我个人而言,我不喜欢将属性设置为除了更改值之外会产生太多后果,所以我倾向于坚持Start()/Stop()因为我很清楚(呃)当你调用一个方法时,你是在要求一些东西发生。

That said, I don't suppose there is a whole lot of ambiguity about what setting Enabled = trueis going to do :)

也就是说,我不认为关于设置Enabled = true将要做什么有很多歧义:)

回答by Ahmad

I don't use timer.Stop()and timer.Start(), because they are subs of timer.Enabled. If you want to set the timer to false at the beginning of the application (at loading) , you must used timer.Enabled = false, timer.Stop()won't work. This is why I use timer.Enabled = false/true.

我不使用timer.Stop()and timer.Start(),因为它们是timer.Enabled. 如果您想在应用程序开始时(加载时)将计时器设置为 false,则必须使用timer.Enabled = falsetimer.Stop()将不起作用。这就是我使用timer.Enabled = false/true.

回答by Jayson Ragasa

Here's a simple code to test how Enabled, Start(), Stop()work with each other.

这是一个简单的代码,用于测试Enabled, Start(), 之间如何Stop()协同工作。

Make a test Windows form app, add two simple buttons and paste this code inside Form1()constructor:

制作一个测试 Windows 窗体应用程序,添加两个简单的按钮并将此代码粘贴到Form1()构造函数中:

int c = 0;
Timer tmr1 = new Timer()
{
    Interval = 100,
    Enabled= false
};
tmr1.Tick += delegate
{
    c++;
};

// used to continously monitor the values of "c" and tmr1.Enabled
Timer tmr2 = new Timer()
{
    Interval = 100,
    Enabled = true
};
tmr2.Tick += delegate
{
    this.Text = string.Format("c={0}, tmr1.Enabled={1}", c, tmr1.Enabled.ToString());
};

button1.Click += delegate
{
    tmr1.Start();
};
button2.Click += delegate
{
    tmr1.Stop();
};