依靠API函数可以实现: Declare Sub Sleep Lib "kernel32" _ (ByVal dwMilliseconds As Long) '延迟1秒 Call Sleep(1000)
private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Sleep 1000
sleep 将会使进程挂起,具体表现为sleep过程中程序没有响应下面函数利用 VB 的 doevents ,没有这个现象,推荐使用: ================================== Public Sub Delay(mSec As Long) On Error GoTo ShowErr
Dim TStart As Single
TStart = Timer
While (Timer - TStart) < (mSec / 1000) DoEvents Wend Exit Sub ShowErr: MsgBox Err.Source & "------" & Err.Description Exit SubEnd Sub 精确到毫秒: delay 1000 '延时1秒
'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 5 sec." 'Sleep for 5000 milliseconds Sleep 5000 Me.Caption = "" End Sub Private Sub Form_Load() Me.Caption = "" Command1.Caption = "Sleep ..." End Sub
Private Declare Function GetCurrentTime Lib "kernel32" Alias "GetTickCount" () As Long Dim LngOld As Long LngOld = GetCurrentTime Do While (GetCurrentTime - LngOld) <= 3000 '这里的3000可以自己改。 DoEvents Loop
Declare Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As Long)
'延迟1秒
Call Sleep(1000)
==================================
Public Sub Delay(mSec As Long)
On Error GoTo ShowErr
Dim TStart As Single
TStart = Timer
While (Timer - TStart) < (mSec / 1000)
DoEvents
Wend
Exit Sub
ShowErr:
MsgBox Err.Source & "------" & Err.Description
Exit SubEnd Sub
精确到毫秒: delay 1000 '延时1秒
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
Private Sub Form_Load()
Me.Caption = ""
Command1.Caption = "Sleep ..."
End Sub
Dim LngOld As Long
LngOld = GetCurrentTime
Do While (GetCurrentTime - LngOld) <= 3000 '这里的3000可以自己改。
DoEvents
Loop