bently5150
Instructor
- Oct 29, 2000
- 4
Any advice on using an event instead of polling? I do a lot of polling in my application.
For one, I have to check Reaction Time buttons to see if they are being pressed. It used to really chew up the CPU until I introduced Sleep to my polling loop.
I am already familiar with the timer control and I like it okay I guess. My sleepy loop works fine too, and spares the CPU some work.
But boy, wouldn't it be neat to have a button pressed event just like you get a mouse-click event.
Here's a code sample for how I poll for a rection time button.
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Public Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Private Function Button_Pressed(lWait_Time as Long) as Long
'returns elapsed time when button is pressed
'times out after lWait_Time
Dim lStart_Time as Long
Dim lElapsed_Time as Long
lStart_Time = timeGetTime()
Do Until Button_Pressed or lElapsed_Time > lWait_Time
lElapsed_Time = timeGetTime() - lStart_Time
If Get_Button_State() = bsPRESSED Then
Button_Pressed = lElapsed_Time
End If
Sleep 10 'sleep 10 msec, let CPU rest
Loop
End Function
I suppose I could write a class to raise the event when the button is pressed and then consume the event in my app, but I still have to poll in the class, right?
Any tips much appreciated!
Thanks,
Bently
For one, I have to check Reaction Time buttons to see if they are being pressed. It used to really chew up the CPU until I introduced Sleep to my polling loop.
I am already familiar with the timer control and I like it okay I guess. My sleepy loop works fine too, and spares the CPU some work.
But boy, wouldn't it be neat to have a button pressed event just like you get a mouse-click event.
Here's a code sample for how I poll for a rection time button.
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Public Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Private Function Button_Pressed(lWait_Time as Long) as Long
'returns elapsed time when button is pressed
'times out after lWait_Time
Dim lStart_Time as Long
Dim lElapsed_Time as Long
lStart_Time = timeGetTime()
Do Until Button_Pressed or lElapsed_Time > lWait_Time
lElapsed_Time = timeGetTime() - lStart_Time
If Get_Button_State() = bsPRESSED Then
Button_Pressed = lElapsed_Time
End If
Sleep 10 'sleep 10 msec, let CPU rest
Loop
End Function
I suppose I could write a class to raise the event when the button is pressed and then consume the event in my app, but I still have to poll in the class, right?
Any tips much appreciated!
Thanks,
Bently