Timer Resolution

Posted on 2003-02-25
Medium Priority
Last Modified: 2012-05-04
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.

Question by:desmclean
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8018555
The Windows QueryPerformanceCounter function can retrive more accurate timing information than GetTickCount.  To determine how accurate, call QueryPerformanceFrequency.

Help for QueryPerformanceCounter:

Help for QueryPerformanceFrequency:

Accepted Solution

Smashmad earned 150 total points
ID: 8023277
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question