So I am working on someone else’s code for a scheduler type program. He creates a timer to run every 10 seconds to check a list of schedules to see if any has to be run. I modified to update the Schedule.button.forecolor depending on if there are schedules or if any are currently running (yellow and green, respectively). The code for the timers are on the schedule form file
The problem is that the timers are called on project startup, which makes sure that a user doesn’t have to actually click on the scheduler form to actually start the timer. But when this timer is called on startup, because the timer is on the form code, an instance of this form is made to make the timer start. Of course, later when the page is accessed, it is a different instance so the buttons will not update.
How can I resolve this problem? Should I refactor the old code to separate the timer instance in a different file, then call it? Or is there a better way to start the timer on startup?
For the sake of single responsibility I’d suggest to seperate the timer code from all the UI. You could raise a custom CheckForUpdates event at the appropiate time (in the timer class), which gets caught in every mask/instance, which needs button updates.
That makes your code more extensible as well.
Edit: is there a database implied in your project as well? It seems to me like the timer checks every ten seconds, if there are any tasks to be run. It would be more efficient if this check was serverside. And everytime a task has to be run, the server sends a message to the client.