定时器的间隔时间是从定时器执行完成后计算的,但我感觉如果定时器里面的执行代码运行的时间比间隔时间长的话, 就会出现问题, 不知道有没有人遇到过样的问题,或者有谁有这方面的经验.

解决方案 »

  1.   

    “定时器里面的执行代码运行”运行之前
    timer1.enable = false“定时器里面的执行代码运行”完后,
    timer1.enable = true
      

  2.   

    试验了一下这两种代码.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控件就不算代码的执行时间而直接用的是原本的间隔时间.
      

  3.   

    也想过用一个定时器执行一个死循环函数, 用Delay函数等待.
      

  4.   

    关于时间控制的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
      

  5.   

    最后方案是试试一楼的这种方法.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
      

  6.   

    要停止啊。TIMER.ENABLE=FALSE