本人新手,从网上下载了一个VB的倒计时软件源码,但经过修改后发现超时计算的时间不准,以下是代码,求指导。
Private Sub Timer1_Timer()                                                      'C+Z/X
    Static I As Long
    If I = 1200 Then I = 0
    If I Mod 10 = 0 Then
        Seconds = CInt(Seconds) + 1
        
        If Seconds = "60" Then
            Seconds = "00"
            Minutes = CInt(Minutes) + 1
            Debug.Print Minutes
        End If
        If Minutes = "60" Then
            Minutes = "00"
            Hours = CInt(Hours) + 1
        End If
        
        myTime = Format(Hours, "00") & ":" & Format(Minutes, "00") & ":" & Format(Seconds, "00")
        FrmStr = "超时:" & myTime
        Call FrmTimer
    End If
    I = I + 1
    If Hotkey(vbKeyControl, vbKeyZ) = True Then Form1.Show
    If Hotkey(vbKeyControl, vbKeyX) = True Then frmLCD.Show

解决方案 »

  1.   

    要精确计时,可以用 Timer() 函数,获得从午夜开始到现在经过的秒数,两位小数。
    在定时器中计算当前和启动时的 Timer() 差值,就是经过秒数。
      

  2.   

    我觉得是“积累误差”的问题。
    系统根本不保证定时器控件的Timer()事件精确触发。
    你的“时间累计”时间长了,很可能就产生误差。我认为,还是按系统时间来进行计算,结果才比较可靠。
    在“开始时计”时,把当时的系统时间记录下来(也就是:起点时间),
    然后,在你的程序界面中更新显示时,按当前时间与起点时间的差值,
    在程序界面中计算出相应的计时值。
    这种方法,只是在处理过子夜和跨日的情况下,逻辑上复杂点儿,
    如果是“当天中的计时”,也很简单。
    这种方式,肯定不会出现楼主说的那种现象。
      

  3.   

    timeGetTime
    The timeGetTime function retrieves the system time, in milliseconds. The system time is the time elapsed since Windows was started.DWORD timeGetTime(VOID); 
    Parameters
    This function does not take parameters.Return Values
    Returns the system time, in milliseconds.Res
    The only difference between this function and the timeGetSystemTime function is that timeGetSystemTime uses the MMTIME structure to return the system time. The timeGetTime function has less overhead than timeGetSystemTime.Note that the value returned by the timeGetTime function is a DWORD value. The return value wraps around to 0 every 2^32 milliseconds, which is about 49.71 days.This can cause problems in code that directly uses the timeGetTime return value in computations, particularly where the value is used to control code execution. You should always use the difference between two timeGetTime return values in computations. Windows NT: The default precision of the timeGetTime function can be five milliseconds or more, depending on the machine. You can use the timeBeginPeriod and timeEndPeriod functions to increase the precision of timeGetTime. If you do so, the minimum difference between successive values returned by timeGetTime can be as large as the minimum period value set using timeBeginPeriod and timeEndPeriod. Use the QueryPerformanceCounter and QueryPerformanceFrequency functions to measure short time intervals at a high resolution, Windows 95: The default precision of the timeGetTime function is 1 millisecond. In other words, the timeGetTime function can return successive values that differ by just 1 millisecond. This is true no matter what calls have been made to the timeBeginPeriod and timeEndPeriod functions. QuickInfo
      Windows NT: Requires version 3.1 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Unsupported.
      Header: Declared in mmsystem.h.
      Import Library: Use winmm.lib.See Also
    Multimedia Timers Overview, Multimedia Timer Functions, timeGetSystemTime, MMTIME, timeGetTime, timeBeginPeriod, timeEndPeriod,QueryPerformanceCounter,QueryPerformanceFrequency  
      

  4.   

    VB 的定时器是控件,和其他控件一样有一个空闲时响应的前提。
    如果代码进入长时间运算状态,界面就不会有响应;
    同样如果 FrmTimer 的执行时间原大于定时间隔,那么定时消息丢弃几个很正常。
    所以不要把定时消息做实时的计时用,要用 Timer()、Now() 之类的函数取时间计算差值。