Here is a trivial example using dispatchEvent to show the synchronous 
invocation of listeners that could (if badly written) interfere with 
innocuous code.

Normally when the "click to count" button is pressed, the count goes from 1 
to 10.
If the button to enable despatching the event is pressed, when the counter 
reaches 5 an event is despatched. The event handler updates the counter, 
adding 10 to it's value.
The output shows that despatched events do indeed invoke the listeners 
synchronously and as this example shows, the listeners can interfere with 
the smooth running of code if they are badly written.

Trivial example, certainly, but it shows the principle.

Paul

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="vertical" 
applicationComplete="init()">
<mx:Script>
<![CDATA[
public const UPSET_COUNTER_EVENT:String = "upsetCounterEV";
public var counter:int;
public var counterEventEnabled:Boolean = false;
public function init():void {
this.addEventListener(UPSET_COUNTER_EVENT, eventListener);
}
public function eventListener(ev:Event):void {
counter += 10;
}
public function clickHandler():void {
counter=1;
for (var i:int =1; i<11; i++){
ta.text+="counter="+counter+"\n";
if ((i==5) && (counterEventEnabled)){
this.dispatchEvent(new Event(UPSET_COUNTER_EVENT));
}
counter++;
}
}
]]>
</mx:Script>
<mx:TextArea id="ta" height="200" width="200"/>
<mx:Button label="click to enable event" click="counterEventEnabled=true" />
<mx:Button label="click to count" click="clickHandler()" />
</mx:Application> 

Reply via email to