Notes for using timer (. NET)

Source: Internet
Author: User

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:

    1. 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 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.