How do I loop when button clicked until other button clicked?

I have made an interface with 3 buttons: start, stop, and exit, and when start is clicked would like to perform a loop in the function until stop or exit is clicked. How do i get the function which is started by the start button to stop if one of the other two buttons is clicked?

def draw():
   draw buttons

def event(evt, val):
   do stuff

def bevent(evt):
   if evt == EVENT_START:
      while evt == EVENT_START:
         obj.RotX = x 
         RedrawAll
         sleep(30)
   elif evt == EVENT_EXIT:
      Exit()

Register(draw,event,bevent)

IMO there is a lack of functionality in B2.5 that supports this kind of thing well. The only viable option I can think of at the moment is to start a python thread that watches the states of flags which are set and cleared by those ‘stop’ and ‘start’ buttons. The disadvantage in using a thread is that many Blender data objects cannot be written by the thread (you’ll get an error msg along the lines of ‘not writable in this context’). However there might be some way to offload the writing of such objects to the ‘stop’ button operator, but timing might be touchy.

There is also a timer event in B2.56 but I couldn’t find any documentation on it and couldn’t get it to work. I guess its not finished yet.

…or just make a modal operator that quits when it sees a certain keypress.

There’s examples in the text editor->text->templates menu.

Thanks, I have just installed v2.56 and will give your suggestions a try.