Windows service with multiple timers

I was provided with a Windows service written by a previous intern during my current internship, which monitors the archive and alerts specific people via email and pop-ups if one of the recorded values โ€‹โ€‹falls outside a certain range. He currently uses a timer to check the archive every 30 seconds, and they asked me if I could update it to choose a time that depends on what the tag is being tracked. It uses an XML file to track which tags are being tracked. Would creating multiple timers in a service be the most efficient way around this? I'm not quite sure which approach to take.

The service is written in C # using .NET 3.5.

+4
source share
3 answers

Depending on the granularity, you can use one timer, which is a common factor in the time intervals they want. Suppose they want to put every archive in an XML file that needs to be checked every few minutes. You set up a timer that turns off once a minute, and you check how much time has passed since you did this and whether or not to do it.

If you have the opportunity to redesign, I would move away from the service to a set of scheduled tasks. Write it so that one task does one archive. Then write a controller program that configures scheduled tasks (and can stop them, change them, etc.). The API for scheduled tasks in Windows 7 is good and understandable, and unlike a service, you can enter restrictions, such as โ€œdo not do thisโ€ if the computer is turned on or โ€œonly do this if the machine is in standby modeโ€ together with your preferences for what to do if you missed the opportunity to run the task.7 or 8 scheduled tasks, each according to its schedule, using the same API that goes along the archive path and email address is much neater than one service trying to manipulate everything with In addition, the machine will start faster if you do not already have an autostart service.

+2
source

Effective? Maybe not - especially if you have a lot of tags, since each timer takes up a small but limited amount of resources.

An alternative approach could be a single timer that fires every second, and when that happens, you check the list of outstanding requests.

It may be easier to debug if everything goes wrong with just one active thread.

As with most code maintenance cases, it depends on your existing code, your capabilities, and how you feel more comfortable.

+1
source

I would suggest just using a single timer planned for the smallest common factor.

For example, set the timer to alarm every second, and you can process each interval (1 second, 2 seconds, ...) by counting the corresponding number of timer ticks.

+1
source

Source: https://habr.com/ru/post/1380583/


All Articles