Let's say function foo() is executing. Suppose that an external event occurs, for which you have a handler. Will function foo() be interrupted so that the event handler can be executed? What is the order of execution in this situation?
This actually can be sorta tricky.
I don't really know how Flash works in this regard, but there are a couple of cases in C#, for example. I suggest you read up on how this stuff works. Also good is to break in a debugger and examine the stack that led to your event handler to get a sense for it.
Basically there's one of two scenarios going on:
Like Dr_Asik says, you could be directly invoking a delegate event, which is exactly like calling a method synchronously. In this case, the normal rules about thread context-switching apply, but an event has no special properties, really. It is just a function call.
You could be talking about UI Forms events. In this case, some special rules do apply. A UI event gets "posted" to the event queue, rather than being executed synchronously. So if foo() is on the "Main" UI thread, then the user presses a key while foo() is running, then the keypress is trapped by the OS and posted to the application's UI event queue. But foo() is already running, so the main thread will not stop and check that queue. Only once foo()'s entire call stack completes all the way back to the message queue checking loop will the main thread find that message, process it, and invoke its handler.
To be clear, in that last case foo() is guaranteed not to be interrupted by the UI event.
However, in case 2 there is another scenario, where foo() is not on the main UI thread. In this case, it can absolutely be interrupted, by normal context switching.
In C# it can get even trickier if you start using Control.Invoke().
This isn't Flash-specific but I hope it helps. I suspect Flash has a main thread with an event queue and a processing loop, just like C#. It's a common model that you find in at least C#, Java, and Delphi.
No, foo()
will not be interrupted.
Flex is single-threaded, so foo()
will continue running. Once foo() finishes and control is returned to the event loop, then the first event in the event queue will be processed.