简体   繁体   English

启用已经启用的计时器

[英]Enabling an already enabled timer

I have a program that functions as a monitor software for my arduino motion detector and I am requesting sensor data through timer. 我有一个程序用作arduino运动检测器的监视软件,并且正在通过计时器请求传感器数据。 I have made it so the alarm has to be reset explicitly through a button. 我已经做到了,所以必须通过按钮明确地重置警报。 My question is therefore; 因此,我的问题是; do I want to set the enabling of the timer to false, before changing the detection variable, then enabling it? 我想在更改检测变量然后将其启用之前将计时器的启用设置为false吗?

tl;dr What happens when you enable an already enabled timer? tl; dr启用已启用的计时器时会发生什么? Is it bad or does it just reset? 是不好还是只是重置?

Setting the Enabled property on an instance of System.Windows.Forms.Timer does not reset the timer. 在System.Windows.Forms.Timer实例上设置Enabled属性不会重置计时器。 The state of the timer only changes if the value that you assign to Enabled is different to the current value, ie: currently False and you set it to True or currently True and you set it to False. 仅当您分配给Enabled的值与当前值不同时,计时器的状态才会改变,即:当前为False,并将其设置为True或当前为True,并将其设置为False。

Behind the scenes, the first thing that happens is an exclusive lock is aquired on the object. 在幕后,首先发生的事情是在对象上获得了排他锁。 In .Net 2 and 3.5 this is done through the use of a Lock Statement . 在.Net 2和3.5中,这是通过使用Lock语句来完成的 In .Net 4 this is done using the Monitor Class or more specifically the Monitor.Enter() method. 在.Net 4中,这是使用Monitor类或更具体地说Monitor.Enter()方法完成的。 Once a lock has been successfully aquired the check is performed to determine if the newly assigned value of Enabled is different to the current value. 成功获取锁后,将执行检查以确定新分配的Enabled值是否不同于当前值。

This information was gained by decompiling the System.Windows.Forms.dll assembly. 通过反编译System.Windows.Forms.dll程序集获得此信息。 I would have posted the code snippet but I am unsure as to the legality of doing so. 我会发布代码段,但是我不确定这样做的合法性。

In summary, it is ok to set the timer to Enabled = true when it is already enabled however you do need to be aware that exclusive aquisition of the object is being requested every time you do so. 总而言之,可以将定时器设置为Enabled = true(如果已启用),但是您需要知道,每次执行此请求时,都要求请求对象的独占。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM