question:how to exit a loop when press an button

Hello,erveryone.

I have a question describe as follows:
I lay two buttons in a window.one is “start”,the other one is “stop”.when i press "start " button, it get into a for-loop .I want to realize that if i press the “stop” button, it exit for-loop.How can I do for this?

At first,I set a bool variant.when i press “stop”,i set the bool false.In the for loop,I check the bool variant.If true,continue circulating.If false,I want it stop.But when it get into the for-loop,the "stop"button invalid.

What methods I can use to realize the function above?Please guide me.Thanks.

That won’t work. Photon is an event based system, event can only be received while in the main loop, which then gets process and ultimately ends up invoking your callback. While running code in the callback events cannot be processed You must change your design. There is a section in the document about dealing with long process.

You could do this if the loop is started in a thread. Assuming that the loop is cpu bound, it
could not be at a higher priority, or the stop code would never run.

Instead of calling a for loop in the button’s callback, have the “start” set the same bool. Drop a PtTimer widget on screen; it gets called like a while(1). Replace the for statement with an if; the code inside the block is the same.

Note that even at a 1msec callback rate you have a delay on each “loop.”

-James Ingraham
Sage Automation, Inc.

Yeah somebody should have flag that “endless loop” are a no no ;-)