怎样在程序中加入一定长度的延时

wenli 2000-08-20 11:25:00
加精
怎样在程序中加入一定长度的延时,我现在用的是
Private Sub Delay(lDalayTime As Long)
Dim lCurrtime As Long, lLasttime As Long
lCurrtime = GetTickCount + lDalayTime
Do While GetTickCount < lCurrtime
Loop
End Sub
但是资源占用太历害。而且在多任务的情况下不能很好的工作。
在foxpro中有WAIT语句可用来延时,VB中怎样较精确的实现?
且不占用太多资源。
我还试过用时钟控件进行延时处理,但是TIME事件中因些会增加太多的判断
语句使程序运行速度得不到保证,我想知道各位是怎样让和序延时运行的。
...全文
237 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sunrain 2001-11-18
  • 打赏
  • 举报
回复
要精确到1ms级用我的控件SRTimer
http://www.eleauto.com

精确到25ms以上用这个
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sleep 1000 '1000是毫秒
ticsa 2000-12-28
  • 打赏
  • 举报
回复
用timer控件可以吗?
OUYAN 2000-08-21
  • 打赏
  • 举报
回复
用api函数:
Public Declare Function timeGetTime Lib "winmm.dll" Alias "timeGetTime" () As Long
timeGetTime能达到1ms精度
shines77 2000-08-21
  • 打赏
  • 举报
回复
哦,忘了API函数声明:) (加在前面:)

Private Declare Function timeGetTime Lib "winmm" () As Long
shines77 2000-08-21
  • 打赏
  • 举报
回复
我给你一个函数,精确到微秒级,如:Pause(1000),暂停一秒,而且程序仍然可以响应任何事件,因为有DoEvents语句,很秒哦,弥补了VB的不足。

Function BetterNow() As Date
Dim UpTimeMsNew As Long
Static OffSet As Date
Static UpTimeMsOld As Long
Const OneSecond = 1 / (24# * 60 * 60)
Const OneMs = 1 / (24# * 60 * 60 * 1000)

UpTimeMsNew = timeGetTime()
If OffSet = 0 Or UpTimeMsNew > 1000000000 Then
OffSet = Date - UpTimeMsNew * OneMs + CDbl(Timer) * OneSecond
UpTimeMsOld = UpTimeMsNew
UpTimeMsNew = 0
End If
BetterNow = UpTimeMsNew * OneMs + OffSet
End Function

Public Sub Pause(PauseLength As Long)
Dim TimePrev As Date, TimeNow As Date, WaitTime As Date
WaitTime = PauseLength / (24# * 60 * 60 * 1000)
TimePrev = BetterNow()
Do
TimeNow = BetterNow()
DoEvents
Loop Until (TimeNow - TimePrev) > WaitTime
End Sub
opkj 2000-08-20
  • 打赏
  • 举报
回复
sleep

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧