Timer, as its name implies: timer; inProgramWhen You Need To Do Something regularly, timer is usually our first choice, because timer is really easy to use.
Generally, when we use timer, we only do two things:
- Specify an interval value for the timer in milliseconds (but the accuracy is not that high ).
Here, for example, the value is 2000 (2 seconds)
2. Add the task to be done in the tick event of timer.
Private VoidTimereffectick (ObjectSender, eventargs e ){// Dosomething ();}
Generally, the above is OK.
In the preceding example, set interval to 2000 (2 seconds)
Dosomething () takes 3 seconds or more. What will happen?
the result is that after dosomething is executed, it does not wait 2 seconds, but is executed again directly, it violates our original intention.
the following example can solve the problem to a large extent:
private void timereffectick ( Object sender, eventargs e) {timer1.stop (); // stop timer try { // dosomething (); }< span style =" color: # 0000ff "> finally {timer1.start (); // After the task is finished, start timing again }