Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
sleep好像會全個程序都停起來,有可能會發生錯誤 delay要自組Public Function Delay(Mins%, Secs%, Optional ByRef StopFlag) As Long Dim EndOfDelay EndOfDelay = DateAdd("n", Mins, Now) EndOfDelay = DateAdd("s", Secs, EndOfDelay) Delay = 0 Do While (Now < EndOfDelay) DoEvents If Not IsMissing(StopFlag) Then If StopFlag Then Delay = 1 StopFlag = False Exit Do End If End If Loop End Function 使用例子:Dim StopTheTimer As BooleanPrivate Sub Command1_Click() '開始延時 Dim lRetval& lRetval = Delay(1, 5, StopTheTimer) If lRetval = 0 Then MsgBox "時間到!" Else MsgBox "取消延時!" End If End Sub Private Sub Command2_Click() '取消延時 StopTheTimer = True End Sub
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)调用方法:Sleep(millionSecond)还有也可以引入一个空循环来延迟。 如 dim i as Long for i=1 to 100000 next i
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
我是这么做的,在模块里 Declare Function GetTickCount Lib "kernel32" () As Long '获得系统自开机经过的毫秒数 Sub delay(ii As Integer) '时间延迟 Dim tt As Long tt = GetTickCount() Do DoEvents Loop Until GetTickCount - tt > ii End Sub 在用的时候只要,DELAY(XXX) XXX为毫秒
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
'延时函数 'Call PauseProgram(0.5) '0.5是延时0.5秒,你可以设其它值 Public Sub PauseProgram(ByVal PauseTime As Single) Dim snlTime As Single snlTime = Timer DoEvents Do Until Timer - snlTime > PauseTime Loop End Sub我一直用的,成功的。^_^
我是这样来延时的Dim currSecond As Integer '没有需要要下载的。则延时3秒 currSecond = Second(Time) Do While (Second(Time) - currSecond) < 3 '下面这条语句,是防止currSecond在>=57时出现的Second(Time)-currSecond永远不会>=3的情况。 If (currSecond - Second(Time)) > 0 Then If (currSecond - Second(Time)) <= 57 Then Exit Do DoEvents Loop
delay要自組Public Function Delay(Mins%, Secs%, Optional ByRef StopFlag) As Long
Dim EndOfDelay
EndOfDelay = DateAdd("n", Mins, Now)
EndOfDelay = DateAdd("s", Secs, EndOfDelay)
Delay = 0
Do While (Now < EndOfDelay)
DoEvents
If Not IsMissing(StopFlag) Then
If StopFlag Then
Delay = 1
StopFlag = False
Exit Do
End If
End If
Loop
End Function
使用例子:Dim StopTheTimer As BooleanPrivate Sub Command1_Click() '開始延時
Dim lRetval&
lRetval = Delay(1, 5, StopTheTimer)
If lRetval = 0 Then
MsgBox "時間到!"
Else
MsgBox "取消延時!"
End If
End Sub
Private Sub Command2_Click() '取消延時
StopTheTimer = True
End Sub
如
dim i as Long
for i=1 to 100000
next i
Declare Function GetTickCount Lib "kernel32" () As Long '获得系统自开机经过的毫秒数
Sub delay(ii As Integer) '时间延迟
Dim tt As Long
tt = GetTickCount()
Do
DoEvents
Loop Until GetTickCount - tt > ii
End Sub
在用的时候只要,DELAY(XXX) XXX为毫秒
'Call PauseProgram(0.5) '0.5是延时0.5秒,你可以设其它值
Public Sub PauseProgram(ByVal PauseTime As Single)
Dim snlTime As Single
snlTime = Timer
DoEvents
Do Until Timer - snlTime > PauseTime
Loop
End Sub我一直用的,成功的。^_^
'没有需要要下载的。则延时3秒
currSecond = Second(Time)
Do While (Second(Time) - currSecond) < 3
'下面这条语句,是防止currSecond在>=57时出现的Second(Time)-currSecond永远不会>=3的情况。
If (currSecond - Second(Time)) > 0 Then If (currSecond - Second(Time)) <= 57 Then Exit Do
DoEvents
Loop
我用sleep成功了,但是确实是令整个程序都被中止,这一点比较麻烦。下午揭帖
1 不用sleep
2 不用DoEvents,用了DoEvents可能会产生不可预知的错误
Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long) Call Sleep(1000)
Start = Timer
Do While Timer < Start + 3 ' 3 秒的延时
DoEvents '转让控制权
Loop