'******************************************************* '函数名称:delay '参数:N '返回值: '功能:产生延迟,延迟时间为N '******************************************************* Private Sub Delay(ByVal n As Single) Dim tm1 As Single, tm2 As Single tm1 = Timer Do tm2 = Timer If tm2 < tm1 Then tm2 = tm2 + 86400 If tm2 - tm1 > n Then Exit Do DoEvents Loop End Sub
或者使用API函数: Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)程序中用 sleep 500'延时半秒 就可以了
Public Declare Function timeGetTime Lib "winmm.dll" () As Long Public Sub delay(i As Integer) Dim t1 As Long Dim t2 As Long t1 = timeGetTime While (t2 - t1 < i * 1000) //*1000,那i就是要延时i秒,级别可以自己定 t2 = timeGetTime DoEvents Wend End Sub
用Sleep,那么你的程序就不能相应其他的控制了。直到延迟的时间过去。
'延时程序 Public Sub DelayTime(ByVal nSecond As Integer) Dim EndTime As Date EndTime = DateAdd("s", nSecond, Time) While Time < EndTime DoEvents Wend End Sub
用sleep() Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 括号里面的数字代表微秒 sleep(1000)就是延时1秒
我有一个: Function GetCurTime() As Currency If mInit = False Then If QueryPerformanceFrequency(mFrequency) = 0 Then mFrequency = 0 End If mInit = True End If
If mFrequency <> 0 Then Dim CurCount As Currency Call QueryPerformanceCounter(CurCount) GetCurTime = CurCount * 1000@ / mFrequency 'Debug.Print GetCurTime() Else GetCurTime = GetTickCount() End If
End FunctionSub Delay(ByVal n As Currency) Dim Timer1 As Currency, Timer2 As Currency Timer1 = GetCurTime() Do Timer2 = GetCurTime() If Timer2 - Timer1 > n Then Exit Do DoEvents Loop End Sub
TO DragonSchool(龙堂) : 1.使用sleep的方法在期间你就做不了任何操作 这点我不敢苟同. 你的话有一定的道理,比如延时一秒钟用sleep(1000)的话确实在这一秒钟内什么也干不了 但是如果这样: dim i for i=0 to 100 step 1 sleep(10) doevents next i 这样的话不就解决了吗
Public Function Delay(ms As Long) Do Until i > Val(100 * ms) DoEvents i = i + 1 Loop End Function
做长时间延迟建议用gettickcount(要引入的API) dim begin as long begin = gettickcount'延迟if gettickcount - begin > 5000 then ...... '看当前是否比上次运行gettickcount时过了5秒或许可以用now取时间,然后用datediff()判断时间差以上2法都可以延迟长时间,建议不要用sleep,因为它是让你整个进程sleep掉
使用API函数: Private Declare Function SetTimer Lib "user32" Alias "SetTimer" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long SetTimer 0,0,1000,Addressof 在模块中的函数或过程
使用API函数: Private Declare Function KillTimer Lib "user32" Alias "KillTimer" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long结束时钟。
'函数名称:delay
'参数:N
'返回值:
'功能:产生延迟,延迟时间为N
'*******************************************************
Private Sub Delay(ByVal n As Single)
Dim tm1 As Single, tm2 As Single
tm1 = Timer
Do
tm2 = Timer
If tm2 < tm1 Then tm2 = tm2 + 86400
If tm2 - tm1 > n Then Exit Do
DoEvents
Loop
End Sub
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)程序中用
sleep 500'延时半秒
就可以了
Public Sub delay(i As Integer)
Dim t1 As Long
Dim t2 As Long
t1 = timeGetTime
While (t2 - t1 < i * 1000) //*1000,那i就是要延时i秒,级别可以自己定
t2 = timeGetTime
DoEvents
Wend
End Sub
Public Sub DelayTime(ByVal nSecond As Integer)
Dim EndTime As Date
EndTime = DateAdd("s", nSecond, Time)
While Time < EndTime
DoEvents
Wend
End Sub
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
括号里面的数字代表微秒
sleep(1000)就是延时1秒
Function GetCurTime() As Currency
If mInit = False Then
If QueryPerformanceFrequency(mFrequency) = 0 Then
mFrequency = 0
End If
mInit = True
End If
If mFrequency <> 0 Then
Dim CurCount As Currency
Call QueryPerformanceCounter(CurCount)
GetCurTime = CurCount * 1000@ / mFrequency
'Debug.Print GetCurTime()
Else
GetCurTime = GetTickCount()
End If
End FunctionSub Delay(ByVal n As Currency)
Dim Timer1 As Currency, Timer2 As Currency
Timer1 = GetCurTime()
Do
Timer2 = GetCurTime()
If Timer2 - Timer1 > n Then Exit Do
DoEvents
Loop
End Sub
无非是利用Doevents啊!
1.使用sleep的方法在期间你就做不了任何操作
2.使用循环的方法则在循环期间会占用百分百的CPU.
当然有第三种方式,能克服以上的缺点.不过它是一个DLL来的,有兴趣要的话发邮件给我[email protected]
1.使用sleep的方法在期间你就做不了任何操作
这点我不敢苟同.
你的话有一定的道理,比如延时一秒钟用sleep(1000)的话确实在这一秒钟内什么也干不了
但是如果这样:
dim i
for i=0 to 100 step 1
sleep(10)
doevents
next i
这样的话不就解决了吗
Do Until i > Val(100 * ms)
DoEvents
i = i + 1
Loop
End Function
dim begin as long
begin = gettickcount'延迟if gettickcount - begin > 5000 then ...... '看当前是否比上次运行gettickcount时过了5秒或许可以用now取时间,然后用datediff()判断时间差以上2法都可以延迟长时间,建议不要用sleep,因为它是让你整个进程sleep掉
用法:可以放在timer中,timer触发一次就判断一次时间间隔,这个时候,timer的interval越小,延迟越精确
Private Declare Function SetTimer Lib "user32" Alias "SetTimer" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
SetTimer 0,0,1000,Addressof 在模块中的函数或过程
Private Declare Function KillTimer Lib "user32" Alias "KillTimer" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long结束时钟。