Scene
After clicking OK in the low level warning pop-up window, a timer needs to be implemented, such as monitoring is performed after five minutes.
The implementation idea is to use Timer and then execute a method every second, reduce the number of seconds in the method, and execute the corresponding operation after the countdown is over.
accomplish
But Timer has three
1. Defined in
2. Defined in the class
3. Defined in the class
At the beginning, I used it inside
It is applied in WinForm. It is implemented through the Windows message mechanism, similar to the Timer control in VB or Delphi, and is implemented internally using the API SetTimer. Its main disadvantage is that the timing is inaccurate and there must be a message loop, which the Console Application cannot be used.
Using code example:
Create a new timer class object
_timer = new ();
Set the interval time of execution, in milliseconds
_timer.Interval = 1000;
How to set the interval time to execute
_timer.Tick +=_timer_Tick;
private void _timer_Tick(object sender, EventArgs e) { //Executed business }
Start the timer
_timer.Start();
Stop the timer
_timer.Stop();
However, it was found that this timer was not executed, and the method it executed once a second was not executed. It turned out that it could not be used in the console program
So changed to
Create a new timer object and set the execution interval to 1 second
_timerWaterTank = new (1000);//InstantiationTimerkind,Set the interval time to1000millisecond;
Set execution events of the timer
_timerWaterTank.Elapsed += new (_timerWaterTank_Tick);//Execute events when the time arrives;
Is the settings executed once or continuously
_timerWaterTank.AutoReset = true;//Settings are executed once(false)Still keep executing(true);
Specific execution event method
private void _timerWaterTank_Tick(object sender, EventArgs e) { timer = sender as ; //The number of time seconds to be counted --; if ( <= 0) { //Business executed after the countdown is over = true; = false; = Global.LOW_LEVEL_MONITOR_SECONDS; } }
This allows the timer to execute the method once in one second, and in this method, the number of seconds is reduced by 1, so that the specific service is executed when the number of seconds reaches 0.
Start the timer
= true;
Stop the timer
= false;
The above is the detailed content of c# implementing the timer function. For more information about c# timer, please pay attention to my other related articles!