Private Sub Command1_Click() Print "t1" Timer1.Enabled = True End Sub Private Sub Timer1_Timer() Print "t2" Timer1.Enabled = False End Sub
用timer 控件 来控制,不过这段时间它占用cpu
用Timer好麻烦 用wait函数 具体请看msdn中文版
Static Sub LastSub(LastTime) Dim loopfinisH Const SecondsInday = 24& * 60& * 60 loopfinisH = Timer + LastTime If loopfinisH > SecondsInday Then loopfinisH = loopfinisH - SecondsInday Do While Timer > loopfinisH DoEvents Loop End If Do While Timer < loopfinisH DoEvents Loop End Sub这是一个非常好用的延时函数,参数单位为秒。 还有如果你程序此时不响应任何事件的话,还可以用 Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
'This project needs a button Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Sub Command1_Click() Me.Caption = "Your system will sleep 1 sec." Sleep 1000 Me.Caption = "" End Sub Private Sub Form_Load() Me.Caption = "" Command1.Caption = "Sleep ..." End Sub
Private Declare Function timeGetTime Lib "winmm.dll" () As Long 'Windows API函数,取得系统运行时间Function Waiting(PauseTime As Long) '等待时间 Dim Start As Long Start = timeGetTime Do While (timeGetTime < Start + PauseTime) DoEvents Loop End Function单位:ms
sleep使用后回暂时把程序挂起,我感觉不好。
CreateWaitableTimer, SetWaitableTimer + MsgWaitForMultipleObjects比较麻烦,只是占用的 CPU 时间很少,并且可以响应事件。 时间可以精确到 0.1 微秒(0.0001 毫秒)用在这里似乎有点杀鸡用牛刀,呵呵
可不可以这样子:
dim i as Long For i=0 to 10000
Next i
先建一通用涵数 ' Sleep (1000) ' Sleep for 1 seconds Sub Sleep(ByVal MillaSec As Long, Optional ByVal DeepSleep As Boolean = False) Dim tStart#, Tmr# tStart = Timer While Tmr < (MillaSec / 1000) Tmr = Timer - tStart If DeepSleep = False Then DoEvents Wend End Sub 再在需要的地方 call sleep(5000)'5000指延时5秒
ys = Timer + 1
Do
DoEvents
Loop Until Timer > ys
在command_click 中使timer enabled,
在timer 中执行第二个事件.
sleep()会使整个程序停止响应.
Print "t1"
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Print "t2"
Timer1.Enabled = False
End Sub
来控制,不过这段时间它占用cpu
用wait函数
具体请看msdn中文版
Dim loopfinisH
Const SecondsInday = 24& * 60& * 60
loopfinisH = Timer + LastTime
If loopfinisH > SecondsInday Then
loopfinisH = loopfinisH - SecondsInday
Do While Timer > loopfinisH
DoEvents
Loop
End If
Do While Timer < loopfinisH
DoEvents
Loop
End Sub这是一个非常好用的延时函数,参数单位为秒。
还有如果你程序此时不响应任何事件的话,还可以用
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click() Me.Caption = "Your system will sleep 1 sec." Sleep 1000
Me.Caption = ""
End Sub
Private Sub Form_Load()
Me.Caption = ""
Command1.Caption = "Sleep ..."
End Sub
Dim Start As Long
Start = timeGetTime
Do While (timeGetTime < Start + PauseTime)
DoEvents
Loop
End Function单位:ms
时间可以精确到 0.1 微秒(0.0001 毫秒)用在这里似乎有点杀鸡用牛刀,呵呵
dim i as Long For i=0 to 10000
Next i
' Sleep (1000) ' Sleep for 1 seconds
Sub Sleep(ByVal MillaSec As Long, Optional ByVal DeepSleep As Boolean = False)
Dim tStart#, Tmr#
tStart = Timer
While Tmr < (MillaSec / 1000)
Tmr = Timer - tStart
If DeepSleep = False Then DoEvents
Wend
End Sub
再在需要的地方
call sleep(5000)'5000指延时5秒