View Full Version : Getting millisecs in Visual Basic

11-22-2002, 04:45 PM
I would like to be able to access the time in Visual Basic in millisecs.
I was using the timer control set to an interval of 1, but I found out it doesn't actually fire every .001 seconds.

The program tests reaction time to an event, so I was thinking of subtracting the time the program prompts the user from the time the user responds.

Everyone I ask says it should be easy to find but I am having no luck.


11-22-2002, 10:39 PM
I tried this a couple times and it seems to work. I am not familiar with the first line in this code - can someone give me some more information on it?

Private Declare Function GetTickCount Lib "kernel32" () As Long
Private lngStart As Long

Private Sub cmdStart_Click()
lngStart = GetTickCount()
End Sub

Private Sub cmdStop_Click()
MsgBox CStr(GetTickCount() - lngStart) & " milliseconds"
MsgBox lngStart
End Sub

11-23-2002, 04:08 AM
I'm not 100% sure on this but I think it is everytime windows "ticks" or refreshes. And if you don't know, 1 millisecond is a 1/1000 of a second.

The declare function is to access the windows kernel and obtain the tick count. That code should work.

11-23-2002, 04:59 AM
Does anyone know how reliable this code is? I mean, if Windows is running alot of programs will this code still give me an accurate elasped time?

11-26-2002, 08:21 PM
Well this lander game that I found and significantly improved operates based on the tick count, so test it out. I've found it to be very relieble but you never know.