Usage of the Timer class in C #
About the Timer class in C # There are 3 of timers in C #
1. Definition in System.Windows.Forms
2. defined in the System.Threading.Timer class
3. defined in the System.Timers.Timer class
System.Windows.Forms.Timer is applied to WinForm, which is implemented through the Windows messaging mechanism, similar to the Timer control in VB or Delphi, implemented internally using the API SetTimer. The main drawback is that the timing is imprecise and there must be a message loop that the console application (console application) cannot use.
System.Timers.Timer and System.Threading.Timer are very similar, they are implemented through the. NET Thread pool, light-weight, accurate timing, no special requirements for applications, messages. System.Timers.Timer can also be applied to WinForm, completely replacing the timer control above. Their disadvantage is that they do not support direct drag-and-drop and require manual coding.
Cases:
Using the System.Timers.Timer class
Instantiate the Timer class, set a time interval of 10000 milliseconds;
System.Timers.Timer t = NewSystem.Timers.Timer (10000);
Execution of events at time of arrival;
t.elapsed + = NewSystem.Timers.ElapsedEventHandler (theout);
T.autoreset whether the =true;//setting is executed once (false) or always executed (true);
T.enabled =true;//whether to perform the System.Timers.Timer.Elapsed event;
====================================
I wrote a method of using the System.timer class
Public classBf_checkupdate {Private Static ObjectLockObject =NewObject (); //Define data Check timer Private StaticTimer Checkupdatetimer =NewTimer (); //Check for update locks Private Static intCheckupdatelock =0; /// ///Set data Check timer parameters/// Internal Static voidGettimerstart () {//cycle time (10 minutes)Checkupdatetimer.interval =600000; //Allow timer executioncheckupdatetimer.enabled =true; //Defining callbackscheckupdatetimer.elapsed + =NewElapsedeventhandler (checkupdatetimer_elapsed); //Define multiple loopsCheckupdatetimer.autoreset =true; } /// ///Timer Events/// /// /// Private Static voidCheckupdatetimer_elapsed (Objectsender, Elapsedeventargs e) { //lock Check Update lock Lock(lockobject) {if(Checkupdatelock = =0) Checkupdatelock =1; Else return; } //More code goes here. //How to implement the function concretelyCheck (); //Unlock update check lock Lock(lockobject) {Checkupdatelock=0; } } }
Usage of the Timer class in C #