本人新手,從網上下載了一個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
uj5u.com熱心網友回復:
求指點啊
uj5u.com熱心網友回復:
時間短的話,應該不會差很多。uj5u.com熱心網友回復:
要精確計時,可以用 Timer() 函式,獲得從午夜開始到現在經過的秒數,兩位小數。在定時器中計算當前和啟動時的 Timer() 差值,就是經過秒數。
uj5u.com熱心網友回復:
這個原始碼正常倒計時的時候是沒問題的,就是在超時以后的時間就走的慢了uj5u.com熱心網友回復:
我覺得是“積累誤差”的問題。系統根本不保證定時器控制元件的Timer()事件精確觸發。
你的“時間累計”時間長了,很可能就產生誤差。
我認為,還是按系統時間來進行計算,結果才比較可靠。
在“開始時計”時,把當時的系統時間記錄下來(也就是:起點時間),
然后,在你的程式界面中更新顯示時,按當前時間與起點時間的差值,
在程式界面中計算出相應的計時值。
這種方法,只是在處理過子夜和跨日的情況下,邏輯上復雜點兒,
如果是“當天中的計時”,也很簡單。
這種方式,肯定不會出現樓主說的那種現象。
uj5u.com熱心網友回復:
timeGetTimeThe 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.
Remarks
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
uj5u.com熱心網友回復:
VB 的定時器是控制元件,和其他控制元件一樣有一個空閑時回應的前提。
如果代碼進入長時間運算狀態,界面就不會有回應;
同樣如果 FrmTimer 的執行時間原大于定時間隔,那么定時訊息丟棄幾個很正常。
所以不要把定時訊息做實時的計時用,要用 Timer()、Now() 之類的函式取時間計算差值。
uj5u.com熱心網友回復:
感覺樓主只給了一段前不著村,后不著店的代碼,不好說什么。uj5u.com熱心網友回復:
我感覺樓上說的都挺好的呀,可以考慮uj5u.com熱心網友回復:
換個思路,讀取系統時間計算時間差取得超時時間轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/116775.html
標籤:VB基礎類
