Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Polling vs. Raise Event

Status
Not open for further replies.

bently5150

Instructor
Oct 29, 2000
4
0
0
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
 
I'm guessing that you must have a reason why you don't use KeyPress or KeyDown events. Perhaps you could just tell us what those reasons are so that we don't provide a solution that is inapprpriate.
 
Great question. We have a dual monitor system used by an examiner to test a subject.

Only the examiner has access to the keyboard and primary monitor. The subject uses an actual USB Reaction Time Button hardware device which has an OCX and limited set of function calls. The RT buttons need to be polled to se if the button is pressed.

Also, the subject uses a pen to draw or press buttons on the secondary monitor. And the digitizer I'm using to replace the old light pen no longer shares the pointer with the mouse. So I lose my click event on a button when the pen touches the screen. I have to poll to see if the pen is touching the screen, get the x,y coordinates, then move the pointer there and provide the click, on touchdown. Or draw poll repeatedly for x,y coordinates and draw line segments when subject is drawing in a picture box.

Whole lotta polling goin' on. Life's not easy...:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top