Implement sleep with the timer control and sleep with the timer control

Source: Internet
Author: User

Implement sleep with the timer control and sleep with the timer control

Sometimes we need code execution delay, which requires the Thread. Sleep () method, but this method will cause the interface to be suspended when the main Thread is used. Using the timer control not only achieves code execution delay, but also does not cause any problems.

Assume that we need to calculate the mouse coordinates in the form's MouseMove event, but the execution of the MouseMove event is quite frequent. If every time an event is triggered, the cpu usage will be very high. Is there a way to reduce the number of computations?

First declare the timer control

Private System. timers. timer mTimer; mTimer = new System. timers. timer (200); mTimer. autoReset = false; mTimer. enabled = false; mTimer. elapsed + = new System. timers. elapsedEventHandler (mTimer_Elapsed );

Run the following code in the form MouseMove event:

If (! This. mTimer. Enabled)
{This. mTimer. Enabled = true; this. mTimer. Start ();}

Execute the computing code in the event when the timer time reaches the interval

 

Related Article

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.