Event Handler confusion

I still have not figured out how these event handlers get called and which form new threads or if any. I know how do make new threads in C and Java but no idea how in applescript. Well basically my problem is this: When I execute some code from a button clicked event, my GUI interface freezes up and the user cant move the window or click on other buttons while the code is being executed. So I searched the archives here and found a solution. The post said to call idle handler and put your code there and have a variable that turns it on and off from the button clicked handler. Sounds good and worked fine (no more GUI freezing up anymore). All is well until… I needed to run a timer along with with program. The timer needs the idler handler to run every second to update, but I got other code there calling delay commands that ties up the idle handler for 6 mins per iteration (got other things going on that script is waiting for). So I cant seem to get the idler handler to work with both situations. So my question to you guys is… Is there another approach to this than this mickey mouse method I got here? Is there a way to make a new thread so I can run my timer without being locked up while delay command running. I wish I understood the event structure better in applescript. Any help with this problem would be greatly appreciated.

All event handling is done in the main thread. Traditionally AppleScript handles events on a first-in-last-out basis, where an incoming event interrupts the event currently being handled. I think recent versions might have changed to first-in-first-out, where the current event is completed before the next one is handled; you’d really need to check this yourself though.

AppleScript doesn’t support threads. You’ll need to use some other language.

The obvious solution is to push the lengthy tasks into one or more secondary threads or processes. Neither option is entirely straightforward, however: AppleScript can’t create threads, and Studio isn’t good at two-way communication with other processes. You’re running into fundamental limitations in Studio and AppleScript here, and may want to consider using another language (Java/ObjC/Perl/Python/etc.) for some or all of your application.

Alternatively, if your main problem is the delay commands and accurate timing isn’t important, it might be possible to rewrite that code to eliminate the delays, e.g. by scheduling the remaining operations to be carried out at a later time, dumping those jobs into a priority queue that your idle handler can repeatedly check for due jobs and run any it finds.