I am using a System.Windows.Forms.Timer
component on an application's main form which checks every minute for updates on a database. However, there are a number of possible interactions with the user, such as modal dialogs being thrown for certain operations. In general, I don't want this timer to fire when the user is "busy" or while another database operation is active.
My current approach is to explicitly bracket such "sensitive" blocks of code with a checkDatabaseTimer.Stop()/Start()
pair (using try/finally to ensure the Start
is always called on the way out).
But this seems clunky to me: if any coders of the future make amendments to this form's code, they need to remember to apply this Stop/Start bracket around any code that, for example, prompts the user for an input. It would seem easy to forget to do this.
Is there a better way of handling this? Or is it just something we have to be conscious of because multithreading is like running with scissors, not one of those things you can just pretend is easy and safe?