试验了一下这两种代码.Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)Public Sub Delay(PauseTime As Single) Dim Start As Single
Start = Timer Do While Timer < Start + PauseTime Sleep 1 DoEvents Loop End SubPrivate Sub Timer1_Timer() Delay 5 Debug.Print Now End SubPrivate Sub Timer2_Timer() Sleep 5 Debug.Print Now End Sub 加了doevents之后就是执行完timer里面的代码后再重新算间隔时间, 而直接用sleep阻塞式方法的话, timer控件就不算代码的执行时间而直接用的是原本的间隔时间.
也想过用一个定时器执行一个死循环函数, 用Delay函数等待.
关于时间控制的api,不知道对你有没有用。 Private Declare Function SleepEx Lib "kernel32" (ByVal dwMilliseconds As Long, ByVal bAlertable As Long) As Long Private Declare Function GetTickCount Lib "kernel32" () As Long Private Declare Function CreateWaitableTimer Lib "kernel32" Alias "CreateWaitableTimerA" (ByVal lpSemaphoreAttributes As Long, ByVal bManuallngReset As Long, ByVal lpName As String) As Long Private Declare Function SetWaitableTimer Lib "kernel32" (ByVal lngTimer As Long, lpDueTime As FILETIME, ByVal lPeriod As Long, ByVal pfnCompletionRoutine As Long, ByVal lpArgToCompletionRoutine As Long, ByVal flngResume 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 Sub Timer1_Timer() Timer1.Enabled = False '..............要执行的代码
Timer1.Tag = "end" End SubPrivate Sub Timer2_Timer() If Timer1.Tag <> "running" Then Timer1.Tag = "running" Timer1.Enabled = True Timer1.Interval = 2000 End If End Sub
timer1.enable = false“定时器里面的执行代码运行”完后,
timer1.enable = true
Dim Start As Single
Start = Timer
Do While Timer < Start + PauseTime
Sleep 1
DoEvents
Loop
End SubPrivate Sub Timer1_Timer()
Delay 5
Debug.Print Now
End SubPrivate Sub Timer2_Timer()
Sleep 5
Debug.Print Now
End Sub
加了doevents之后就是执行完timer里面的代码后再重新算间隔时间, 而直接用sleep阻塞式方法的话, timer控件就不算代码的执行时间而直接用的是原本的间隔时间.
Private Declare Function SleepEx Lib "kernel32" (ByVal dwMilliseconds As Long, ByVal bAlertable As Long) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function CreateWaitableTimer Lib "kernel32" Alias "CreateWaitableTimerA" (ByVal lpSemaphoreAttributes As Long, ByVal bManuallngReset As Long, ByVal lpName As String) As Long
Private Declare Function SetWaitableTimer Lib "kernel32" (ByVal lngTimer As Long, lpDueTime As FILETIME, ByVal lPeriod As Long, ByVal pfnCompletionRoutine As Long, ByVal lpArgToCompletionRoutine As Long, ByVal flngResume 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
Timer1.Enabled = False '..............要执行的代码
Timer1.Tag = "end"
End SubPrivate Sub Timer2_Timer()
If Timer1.Tag <> "running" Then
Timer1.Tag = "running"
Timer1.Enabled = True
Timer1.Interval = 2000
End If
End Sub