Using the timer control to implement the sleep effect

Source: Internet
Author: User

Sometimes we need the code to delay execution, which requires the use of thread.sleep () This method, but this method in the main thread use will cause the interface animation. Using the Timer control can not only achieve the effect of delayed execution of code, but also do not have the problem of suspended animation.

Suppose we need to calculate the mouse coordinates in the MouseMove event of the form, but the execution of the MouseMove event is quite frequent, and if each event is triggered, the CPU occupancy rate is very high. Is there any way to reduce the number of calculations?

First declare the Timer control

Privatenew System.Timers.Timer (false new System.Timers.ElapsedEventHandler (mtimer_elapsed);  

Execute the following code in the MouseMove event of the form

if (!  This . mtimer.enabled)
{ thistrue; This . Mtimer.start ();}

Execute the calculation code in the event that the timer time reaches a time interval

Using the timer control to implement the sleep effect

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.