简体   繁体   中英

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

tl;dr What happens when you enable an already enabled timer? 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. 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.

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 . In .Net 4 this is done using the Monitor Class or more specifically the Monitor.Enter() method. 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.

This information was gained by decompiling the System.Windows.Forms.dll assembly. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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