in reply to One shot way to end a sub?

Assuming your program isn't threaded, your cancel buttonpress won't get handled anyways until your long running calculation is over. In fact, your GUI will be totally unresponsive to everything during that period. Either have your loop periodically process at least one event (and you can then add your "canceled" check there), or invert your loop to do it's work in small chunks (this is usually the proper thing to do). Have it being called by a period zero alarm and cancel the alarm when "cancel" gets pressed or when the work is finished (or use an Idle handler depending on whether you consider your work high or low priority).