程序中延时时CPU占有率100%
程序中在上一行代码运行完毕再运行下一行代码需要时间暂停几秒,我用了延时的方法如下:
Private Sub CmdMove_Click()
Dim timeup As Boolean
...................................... '上一行代码
Timer2.Interval = 5000
Timer2.Enabled = False
Timer2.Enabled = True
timeup = False
Do While Not timeup
DoEvents
Loop
...................................... '下一行代码
End Sub
Private Sub Timer2_Timer()
timeup = True
End Sub
发现延时的这5000毫秒中CPU占有率达到100%,如何才能解决这个问题?还有其他更好的办法来延时吗?请教一下.
[解决办法]
Private Declare Sub Sleep Lib "kernel32 " (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
Me.Caption = "Your system will sleep 5 sec. "
'Sleep for 5000 milliseconds
Sleep 5000
Me.Caption = " "
End Sub