Multiple Timers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to create a form with 4 individual stopwatches which i want to stop and start individually using number 1 to 4 on the keyboard. I can get one working, however as you are restricted to one timer per form i am unable to work out how to generate the others. I have tried subforms however you are then unable to control each timer witht the keys as you need set the focus to each subform

Hope someone can hel

regard

Owain
 
Hi,


Do not use timer, they are too imprecise, rather, use "time stamp". As
example (the typing has not been checked)


Public Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type

Public Declare Function QueryPerformanceCounter Lib "kernel32"
(lpPerformanceCount As LARGE_INTEGER) As Long
Public Declare Function QueryPerformanceFrequency Lib "kernel32"
(lpFrequency As LARGE_INTEGER) As Long

Public Type Chrono
Starting AS LARGE_INTEGER
Endinf As LARGE_INTEGER
End Type

Public MyChronos(1 to 4) As Chrono ' define four chronos
------------------------------------------
Public Function LargeToDec(Arg As LARGE_INTEGER) As Variant
Dim temp As Variant
temp = 4 * CDec(1073741824) ' 2 ^ 32
LargeToDec = Arg.lowpart + iif( Arg.LowPart<0, 1, 0) +Arg.highpart) *
temp
End Function
------------------------------------------
Public Function Frequency() As Variant
Static f As Variant
Dim temp As LARGE_INTEGER

If IsNull(f) then
QueryPerformanceFrequency temp
f=LargeToDec temp
End If

Frequency=f
End Function
---------------------------------------------
Public Function ElapsedTime( x As Chrono) As Double
ElapsedTime=Frequency() * ( LargeToDec( x.Starting) -
LargeToDec(x.Ending) )
End Function



So, to start a chrono "i" :

QueryPerformanceCounter MyChronos(i).Starting


To stop it:

QueryPerformanceCounter MyChronos(i).Ending


To get the elapsed time:

ElapsedTime( myChrono( i ) )




It is up to you to maintain the "state" (ie, pushing the key means to start
the chrono or to stop it).


Note that someone can use the API timeGetTime, but it is far much less
precise than using performance counter. Using Timer is very unprecise and
being a low "message", it may loose a tic-tac from time to time, if it don't
fire.




Hoping it may help,
Vanderghast, Access MVP


Owain D said:
I am trying to create a form with 4 individual stopwatches which i want to
stop and start individually using number 1 to 4 on the keyboard. I can get
one working, however as you are restricted to one timer per form i am unable
to work out how to generate the others. I have tried subforms however you
are then unable to control each timer witht the keys as you need set the
focus to each subform.
 
Back
Top