multithreading - Is there a better way to block a C# timer when interacting with the user? -


I am using the System.Windows.Forms.Timer component on the main form of an application Everybody checks for updates on the database, however, there are several possible interactions with the user, such as the modal dialog is being thrown for some tasks. In general, I do not want this timer if the user is "busy" or other database operation is active.

My current approach clearly shows the bracket "sensitive" checkDatabaseTimer.Stop () / start () pair (always try / end start ) Is used to ensure).

But it seems to me a nervousness: If any future code changes the code of this form, then it should be remembered to apply this stop / start bracket around any code, for example For this, the user is prompted for an input, so it seems easy to forget to do so.

Is there a better way to deal with this? Or is it just something we should be aware of because multithreading is like running with a scissor, is not one of those things that you can just argue is easy and safe?

The timer is "start / stop" a boolean Use the value that runs the timer to be checked at the beginning of that method

You can use an assistant class to reduce the "coding overhead" on your methods:

  public class timer enabled flag {public boole ITEMER ENTERED {get} SET;}} Public Class Timer Enablers: IDisposable {Private Timer Enable Flag Flag; Public Timer Enablers (TimerAnnab flag Flag) {this.flag = flag; Flag.IsTimerEnabled = true;} Public Zero Extracting () { Flag.IsTimerEnabled = false;}}  

Now you have only one "global variable" The timer that keeps, enables the flag that you check when you enter the method run by the timer and when you want to disable the timer just add the following code:

  (Var enabler = new timerEnabler (m_flag)) {// ...}  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -