The easy way (however, it may be slow, I don't really know at this point) is to place a call to the function "ProccessSystemEvents()" in the loop. Then if you have, for example, a stop button, it will check for a call to the callback function associated with that button. Use that button to change the condition of your loop to the "don't cycle" setting and then it should exit the loop.
Hope this helps.
-John