Home > Software engineering > [excuse me each teacher program delay in addition to sleep and timegettime better??)
[excuse me each teacher program delay in addition to sleep and timegettime better??)
Time:09-27
Sleep latency will suspend all the procedures, in VB6 this function might put other sub in running also stopped together ~
Timegettime not hang program and very precise, time delay, other code can also run,
But the downside is also very obvious, is high CPU ~
In VB6 I use the timer function, the timer function is not very precise calculation time but low CPU usage ~)
Inside the timer to delay 200 milliseconds and 5000 milliseconds, delay or other sub function during running, so can't use the sleep function
I use the timegettime function but CPU usage is high ~
Consult everybody the teacher, the timer to delay and can't sleep, is there any way to delay but relatively low CPU utilization?
CodePudding user response:
Don't wait delay within the function, but should keep running down, the end of the function directly, And then start a timer, it is time to call this function again, function from the last state to continue execution down,
CodePudding user response:
The Timer interval down such as Private sub timer1_timer () The static c as long C=(c + 1) mod 10 The select case c Case 1 . Case 5 . Case 8 . End the select End sub
CodePudding user response:
The method is very simple The timegettime + sleep The code
Public Function delay (T As Long) Dim Savetime As Long Savetime=timeGetTime ' note at the beginning of timeWhile timeGetTime & lt; Savetime + T 'circular wait Sleep (10) DoEvents' transfer of control Wend End the Function
Timegettime CPU high reason is that he kept in the loop of Reason is that he doesn't sleep card response UI It is good to combine the Less than 200 best in here to sleep (), is greater than the number, or there will be a point card Minimum of 1, but generally don't need to, the actual precision of less than 1 ms.