讨论延时问题?

SuperZhou 2002-06-03 09:59:52
大家看下面的一个延时过程:
Private Sub DelayTime(ByVal intSeconds As Integer)
Dim lngTime As Long

lngTime = Timer + intSeconds
Do Until lngTime < Timer
DoEvents
Loop
End Sub

Timer是返回一个 Single,代表从午夜开始到现在经过的秒数。也就是说在午夜的时候Timer要复位为0。如果在午夜前10秒运行这个函数,而intSeconds为20,那这个延时过程将进入无效循环。大家有没有更好的延时方法,讨论一下.
...全文
55 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mazhayang 2002-06-06
  • 打赏
  • 举报
回复
用api中的 sleep()函数
junwhj 2002-06-06
  • 打赏
  • 举报
回复
Public Sub Wait_S(SecondV As Single)
Dim i As Single, j As Single
i = Timer + SecondV
Do While i > Timer
If Timer < 1 And i >= 86400 Then '跨0点处理
i = i - 86400
End If
DoEvents
Loop
End Sub
junwhj 2002-06-06
  • 打赏
  • 举报
回复
Public Sub Wait_S(SecondV As Single)
Dim i As Single, j As Single
i = Timer + SecondV
Do While i > Timer
If Timer < 1 And i >= 86400 Then '跨0点处理
i = i - 86400
End If
DoEvents
Loop
End Sub
孙小雄 2002-06-04
  • 打赏
  • 举报
回复
对不起 我没看题目

我想 timer 最大值 是86400

应该有办法
孙小雄 2002-06-04
  • 打赏
  • 举报
回复
********************************************************
我的好使

PauseTime = 5 ' 设置暂停时间。
Start = Timer
Do While Timer < Start + PauseTime
DoEvents
Loop
msgbox "!"
Alsen 2002-06-04
  • 打赏
  • 举报
回复
gz
SuperZhou 2002-06-04
  • 打赏
  • 举报
回复
GetTickCount函数有没有复位的时候?
Random 2002-06-04
  • 打赏
  • 举报
回复
Private Sub DelayTime(ByVal intSeconds As Integer)
Dim timeNow as date

timeNow = dateadd("s",intseconds,now)
Do until timeNow < now
doevents
Loop
End Sub
shawls 2002-06-03
  • 打赏
  • 举报
回复
延时函数下

'************************************************************************************

Public hTimer As Long '定时器句柄


Public Sub Wait(lNumberOfSeconds As Long)
Dim ft As FILETIME
Dim lBusy As Long
Dim lRet As Long
Dim dblDelay As Double
Dim dblDelayLow As Double
Dim dblUnits As Double

Dim ErrCode As Long

On Error GoTo HELL

hTimer = CreateWaitableTimer(0, True, App.EXEName & "Timer")

If Err.LastDllError = ERROR_ALREADY_EXISTS Then

ErrCode = GetLastError()
If ErrCode <> 0 Then
App.StartLogging "Modinitial.Wait 418 Sysrem Error, Code: " & ErrCode,vbLogEventTypeInformation

Err.Clear
End If
' If the timer already exists, it does not hurt to open it
' as long as the person who is trying to open it has the
' proper access rights.
Else
ft.dwLowDateTime = -1
ft.dwHighDateTime = -1
lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, 0)
End If

' Convert the Units to nanoseconds.
dblUnits = CDbl(&H10000) * CDbl(&H10000)
dblDelay = CDbl(lNumberOfSeconds) * 1000 * 10000

' By setting the high/low time to a negative number, it tells
' the Wait (in SetWaitableTimer) to use an offset time as
' opposed to a hardcoded time. If it were positive, it would
' try to convert the value to GMT.
ft.dwHighDateTime = -CLng(dblDelay / dblUnits) - 1
dblDelayLow = -dblUnits * (dblDelay / dblUnits - _
Fix(dblDelay / dblUnits))

If dblDelayLow < CDbl(&H80000000) Then
' &H80000000 is MAX_LONG, so you are just making sure
' that you don't overflow when you try to stick it into
' the FILETIME structure.
dblDelayLow = dblUnits + dblDelayLow
End If

ft.dwLowDateTime = CLng(dblDelayLow)
lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, False)

Do
' QS_ALLINPUT means that MsgWaitForMultipleObjects will
' return every time the thread in which it is running gets
' a message. If you wanted to handle messages in here you could,
' but by calling Doevents you are letting DefWindowProc
' do its normal windows message handling---Like DDE, etc.
lBusy = MsgWaitForMultipleObjects(1, hTimer, False, _
INFINITE, QS_ALLINPUT&)
DoEvents
Loop Until lBusy = WAIT_OBJECT_0

' Close the handles when you are done with them.
CloseHandle hTimer

Exit Sub

HELL:

ErrCode = GetLastError()
App.StartLogging "Modinitial.Wait 476 Sysrem Error, Code: " & ErrCode,vbLogEventTypeInformation
Err.Clear
Resume Next

End Sub


以上代码来自: SourceCode Explorer(源代码数据库)
复制时间: 2002-06-03 23:45:26
当前版本: 1.0.701
作者: Shawls
个人主页: Http://Shawls.Yeah.Net
E-Mail: ShawFile@163.Net
QQ: 9181729
shawls 2002-06-03
  • 打赏
  • 举报
回复
延时函数上

Option Explicit

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Const WAIT_ABANDONED& = &H80&
Private Const WAIT_ABANDONED_0& = &H80&
Private Const WAIT_FAILED& = -1&
Private Const WAIT_IO_COMPLETION& = &HC0&
Private Const WAIT_OBJECT_0& = 0
Private Const WAIT_OBJECT_1& = 1
Private Const WAIT_TIMEOUT& = &H102&

Private Const INFINITE = &HFFFF
Private Const ERROR_ALREADY_EXISTS = 183&

Private Const QS_HOTKEY& = &H80
Private Const QS_KEY& = &H1
Private Const QS_MOUSEBUTTON& = &H4
Private Const QS_MOUSEMOVE& = &H2
Private Const QS_PAINT& = &H20
Private Const QS_POSTMESSAGE& = &H8
Private Const QS_SENDMESSAGE& = &H40
Private Const QS_TIMER& = &H10
Private Const QS_MOUSE& = (QS_MOUSEMOVE _
Or QS_MOUSEBUTTON)
Private Const QS_INPUT& = (QS_MOUSE _
Or QS_KEY)
Private Const QS_ALLEVENTS& = (QS_INPUT _
Or QS_POSTMESSAGE _
Or QS_TIMER _
Or QS_PAINT _
Or QS_HOTKEY)
Private Const QS_ALLINPUT& = (QS_SENDMESSAGE _
Or QS_PAINT _
Or QS_TIMER _
Or QS_POSTMESSAGE _
Or QS_MOUSEBUTTON _
Or QS_MOUSEMOVE _
Or QS_HOTKEY _
Or QS_KEY)

Private Declare Function CreateWaitableTimer Lib "kernel32" _
Alias "CreateWaitableTimerA" ( _
ByVal lpSemaphoreAttributes As Long, _
ByVal bManualReset As Long, _
ByVal lpName As String) As Long

Private Declare Function OpenWaitableTimer Lib "kernel32" _
Alias "OpenWaitableTimerA" ( _
ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal lpName As String) As Long

Private Declare Function SetWaitableTimer Lib "kernel32" ( _
ByVal hTimer As Long, _
lpDueTime As FILETIME, _
ByVal lPeriod As Long, _
ByVal pfnCompletionRoutine As Long, _
ByVal lpArgToCompletionRoutine As Long, _
ByVal fResume As Long) As Long

Private Declare Function CancelWaitableTimer Lib "kernel32" ( _
ByVal hTimer As Long)

Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" ( _
ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long

Private Declare Function MsgWaitForMultipleObjects Lib "user32" ( _
ByVal nCount As Long, _
pHandles As Long, _
ByVal fWaitAll As Long, _
ByVal dwMilliseconds As Long, _
ByVal dwWakeMask As Long) As Long

Private Declare Function GetLastError Lib "kernel32" () As Long




以上代码来自: SourceCode Explorer(源代码数据库)
复制时间: 2002-06-03 23:45:14
当前版本: 1.0.701
作者: Shawls
个人主页: Http://Shawls.Yeah.Net
E-Mail: ShawFile@163.Net
QQ: 9181729
YanJieBing 2002-06-03
  • 打赏
  • 举报
回复

Private Declare Function GetTickCount Lib "kernel32" () As Long

' 功能: 延时
' 参数: mSeconds 毫秒数

Public Function DelayTime(ByVal mSeconds As Long)
Dim lngTime As Long

lngTime = GetTickCount + intSeconds
Do Until lngTime < GetTickCount
DoEvents
Loop
End Function
zyl910 2002-06-03
  • 打赏
  • 举报
回复
Dim SngTime As Single

……
SngTime = Timer
Do Until SngTime + intSeconds < Timer Or SngTime > Timer
……

7,763

社区成员

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

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