Have the GUI set a flag that stops the loop from looping. eg: ``` static int keepgoing; void stopnow() { keepgoing = 0; } void enterthread() { keepgoing = 1; prepare(...) while (keepgoing) ( step(...) reset(...) sleep(...) } finalize(...) } ``` Dispatch enterthread on its own thread. Then just have the GUI change keepgoing to 0 (call stopnow()). Unless of course the statement is taking hours to run ... if it is only taking a few miliseconds then the overhead of even bothering to do anything else is hardly worth it.