interrupt a while loop

  • Thread starter Thread starter r
  • Start date Start date
R

r

I'm doing a robotics experiment in which I have a GUI with 2 buttons. One
button is forward! and the other is reverse!.
The event handler code for each button contains a while loop that will
deliver a timed pulse to a data output on the parallel port which in turn
will run a stepper motor.

The problem is that once in the while loop, the program will no longer
respond to button clicks. I have tried applications.DoEvents() but I really
need a way to break the loop when the user clicks the other button. I think
I need to raise an event and then check for it inside the loop. If this
would work can someone supply a code snippet?

Thanks,
Roger
 
You may be better off using a Timer instead of a timed loop. That way you
can have a state variable that tells the timer's Tick handler what to do,
such as "Forward", "Reverse", or "Nothing". To accomplish this, you'd want
to have the button click handlers set the state variable to the appropriate
state and let the timer's tick handler do all of the work by checking the
current state and acting appropriately..
 
Roger,

Create a new thread and run your code in that. The thread will be separate
to the one that the GUI runs in so the GUI will still be able to react to
any user interaction while your code in the thread is running. For more
information on threading, check out the System.Threading namespace.

Gary
 
Thanks...that certainly moved things in a new direction. I'm checking the
thread tutorial and it looks very straightforward.
 
Thanks...good suggestion.


Ed Kaim said:
You may be better off using a Timer instead of a timed loop. That way you
can have a state variable that tells the timer's Tick handler what to do,
such as "Forward", "Reverse", or "Nothing". To accomplish this, you'd want
to have the button click handlers set the state variable to the appropriate
state and let the timer's tick handler do all of the work by checking the
current state and acting appropriately..
 
Back
Top