Reputation: 154544
When, in ActionScript, an event is dispatched:
foo.addEventListener("some event", someHandler);
foo.dispatchEvent(new Event("some event"));
At what point are the event handlers executed?
I ask because I caught this at the end of an Adobe developer guide:
Notice that some properties are assigned to the [AsyncToken] after the call to the remote service is made. In a multi-threaded language, there would be a race condition where the result comes back before the token is assigned. This situation is not a problem in ActionScript because the remote call cannot be initiated until the currently executing code finishes.
But I could not find any information on what they meant by "currently executing code".
See also: ActionScript event handler execution order
Upvotes: 2
Views: 1541
Reputation: 5242
If you call dispatchEvent()
in ActionScript, the handlers will execute immediately. The order is determined first by the priority specified when you call addEventListener()
and then by the order in which they were added if the priorities are the same. First come, first served.
If an event is dispatched internally from Flash Player, such as Event.COMPLETE
from a URLLoader
instance or anything else that requires network communication, it will not be dispatched while ActionScript is running. It gets queued up for later. I imagine this is precisely to avoid the race condition described in the documentation. I believe it has been observed that "later" is the next frame, but it could happen after all the other ActionScript for the current frame has run.
Upvotes: 5
Reputation: 13984
Actionscript is a single threaded event driven language. Notice how there are no "main" methods in Actionscript. All code belongs in events, eg. initialization code tends to be placed in response to "creationComplete" events. Once the code in that event handler is run, the next event is executed. So if you did:
private function someOtherHandler():void
{
foo.addEventListener("some event", someHandler);
while(true) { ... spin wheels ... }
}
No other handler would be able to run because the currently executing code (the infinite loop) would never complete.
Note that Flash probably uses multiple threads internally, but that is abstracted from the developer.
Upvotes: 4