Using System.Windows.Forms.Timer.Start()/Stop() ve

2019-01-09 05:29发布

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?

标签: c# .net timer
6条回答
forever°为你锁心
2楼-- · 2019-01-09 05:39

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

查看更多
The star\"
3楼-- · 2019-01-09 05:49

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

查看更多
Evening l夕情丶
4楼-- · 2019-01-09 05:52

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.

查看更多
The star\"
5楼-- · 2019-01-09 05:54

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.

查看更多
Bombasti
6楼-- · 2019-01-09 05:58

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.

查看更多
叼着烟拽天下
7楼-- · 2019-01-09 06:02

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

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

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();
};
查看更多
登录 后发表回答