Newbie question - making button presses interrupt processing

P

Peter Webb

I have this animation thing with balls bouncing around. I want to be able to
turn it on and off with button clicks. Trouble is, it doesn't update my form
with the button presses until my animation finishes. This is despite me even
having a thread.sleep(1000) in the main loop which I would expect to let
other processes in.

Is there some way I can force the system to process events either for the UI
as a whole or on a control-by-control basis, so I can check if a Stop button
has been pressed?

I am of course aware that if I (for example) delete objects in the main
thread within my Stop button click handler, all hell could break loose at
runtime, but I promise to use this power wisely.
 
M

Marc Gravell

Thread.Sleep doesn't let anything in - it simply sleeps the current
thread, which I assume is the UI thread; this will make the app
non-responsive.

To respond to events, you might either respond to a timer event to run
each frame successively (yielding to the main message loop in
between), or [cringe] call Application.DoEvents(), which does what you
have described. In general Application.DoEvents() is not recommended -
there are usually "purer" ways of doing the same, but for a throwaway
demo app it may be sufficient for your needs.

Marc
 
P

Peter Webb

Marc Gravell said:
Thread.Sleep doesn't let anything in - it simply sleeps the current
thread, which I assume is the UI thread; this will make the app
non-responsive.

To respond to events, you might either respond to a timer event to run
each frame successively (yielding to the main message loop in between), or
[cringe] call Application.DoEvents(), which does what you have described.
In general Application.DoEvents() is not recommended - there are usually
"purer" ways of doing the same, but for a throwaway demo app it may be
sufficient for your needs.

Marc

Fantastic. One additional line solved my problem. Thankyou.

I could do a frame update on a timer event, but how does this solve my
problem - won't the main program have the same problem reading its buttons?
 
M

Marc Gravell

won't the main program have the same problem reading its

No - as long as you yield between frames, the frames will be waiting
on the same message-pump as the buttons - i.e. the button click here
can fire:

[load]
[start timer]
.... (message pump)
[tick]
draw frame
.... (message pump)
[tick]
draw frame
.... (message pump)
[tick]
draw frame
.... (message pump)
[button click]
[do whatever] [tick]
draw frame
.... (message pump)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top