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