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