Adding a "Cancel" button : slows the process - Any alternative ?

Hi,

Looking for anagrams, I have a recursive handler that is called many times (for example 100000 times for 12 characters with a minimum word length of 7).

I have a “cancel” button (as per AppleScriptObjC Explored 5 - Making progress with cancel) with the following handler called each time the recursive handler is called :

on doEventFetch_()
        repeat
            set theEvent to current application's NSApp's nextEventMatchingMask:(((current application's NSLeftMouseDownMask) as integer) + ((current application's NSKeyDownMask) as integer)) untilDate:(missing value) inMode:(current application's NSEventTrackingRunLoopMode) dequeue:true
            if theEvent is (missing value) then
        exit repeat
        else
        current application's NSApp's sendEvent:theEvent
            end if
    end repeat
        end doEventFetch_

When I deactivate the call to the doEventFetch_ handler, the app runs in 21 seconds (for the example above).
When I activate it, the app runs in 74 seconds.

Any idea to keep a “cancel” button, but keeping performances higher ?

Thanks,
Nicolas

I answer to myself !

I added in the recursive handler a test to call the doEventFetch only once in 1000 times.

This test, even if performed each time, does not penalize the global speed of the app.