2 buttons, 1 loop

iamaldrin08

New member
Joined
Feb 26, 2016
Messages
4
Programming Experience
Beginner
Can anybody give me an idea on how I should proceed here? I have 2 buttons, start-stop, once pressed, calls a loop to start, anytime the loop is running, stop, once pressed.
I have an idea about it like, checking if the stop is pressed but I don't think it's the most efficient.
 
You probably don't actually want a loop at all. Most likely a Timer is the appropriate option. You would Start the Timer in one Button's Click event handler and Stop it in the other's. You would handle the Tick event handler of the Timer and do one thing in there. The Interval of the Timer controls how often the Tick event is raised, allowing you to do that one thing repeatedly.

Alternatively, if what you want to repeat doesn't involve the UI too much, you might use a BackgroundWorker and put a loop in the DoWork event handler. You'd call RunWorkerAsync in the Click event handler of one Button and CancelAsync in the other. You'd then have to check whether the work had been cancelled within the loop.
 
Back
Top Bottom