• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Timer Resolution

I tried implementing the following timer code so I could get a better timer resolution than the inbuilt Timer control in VB which is 55ms. This code measuers the time interval between each timer event. When excuted the resulting time is 110ms on one computer and 55ms on another computer. I need to know is this timer interval correct, Is my code at fault (Is it giving the correct interval). If the code is correct and the timer can only pole at 55ms, it then any other timer that can pole at a higher fequency say 5 to 10 ms.

The following is my timer code ------

In A Module

Public Sub TimerCallback(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
   Call Form1.TimerCall
End Sub

In Form1

Option Explicit
Private Declare Function GetTickCount Lib "Kernel32" () As Long
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Dim Counter As Single
Dim StartTime As Double
Dim FinishTime As Double

Private Sub Command1_Click()

Call SetTimer(Me.hwnd, CLng(1), 50, AddressOf TimerCallback)

End Sub

Private Sub Form_Load()

Counter = 0

End Sub

Public Sub TimerCall()
Dim lngTimerID As Long
Static FirstSD As Boolean
    If Not FirstSD Then
        FirstSD = True
        StartTime = GetTickCount
    End If
Counter = Counter + 1
    If Counter = 2 Then
        Label1.Caption = GetTickCount - StartTime
        KillTimer Me.hwnd, CLng(1)
        FirstSD = False
    End If
End Sub


I would be grateful for any advice regarding this problem.
Is there other way of determining the timers frequency.

1 Solution
The Windows QueryPerformanceCounter function can retrive more accurate timing information than GetTickCount.  To determine how accurate, call QueryPerformanceFrequency.

Help for QueryPerformanceCounter:

Help for QueryPerformanceFrequency:
Windows generates 18 "ticks" per second, so on Any computer, yo will have at least 55.5 ms interval.
What yo can do is a loop between one interval and the other, so you can check how many loops the PC can make in 55 ms.
Then, you can made a loop that delay a few Ms.
I have some code like that.. im trying to find it =)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now