The tried the program below and the output that appears is
 
    1a
    1b
    2
 
indicating that handler1() runs to completion before handler2() starts
executing. And when I set breakpoints on the three t.text assignments in
both handlers, I hit them in the expected synchronous order.
  
> Both alert message boxes are displyed at the same time 
> even though the first handler is still inside the for loop.

 
They're displayed at the same time because the Flash Player doesn't
redraw the stage immediately when you call Alert.show(); it waits until
later, by whicn time all the handlers have completed. I think you were
jumping to the incorrect conclusion that since the two alerts appeared
together, the first handler must have been still inside the loop. Am I
right?
 
- Gordon
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> ">
 
    <mx:Script>
    <![CDATA[
 
    private function doit():void
    {
        addEventListener("doit", handler1);
        addEventListener("doit", handler2);
        dispatchEvent(new Event("doit"));
    }
  
    private function handler1(event:Event):void
    {
        t.text += "1a\n";
        for (var i:int = 0; i < 1000000000; i++)
        { 
        }
        t.text += "1b\n";
    }
 
    private function handler2(event:Event):void
    {
        t.text += "2\n";
    }
  
    ]]>
    </mx:Script>
 
    <mx:Button id="b" click="doit()"/>
 
    <mx:Text id="t"/>
 
</mx:Application>


________________________________

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Jehanzeb Musani
Sent: Wednesday, November 28, 2007 2:16 PM
To: flexcoders@yahoogroups.com
Subject: RE: [flexcoders] Events and Non-Display Objects



Thanks Gordon for you prompt reply.

Gordon, my experience is somewhat different that what
you explained. As per my experience, I feel the
handler functions of listeners are called
asynchronously in the order the handlers were
registered.

By synchornous, I mean if the handler of the first
listener calls a blocking function or perform some
time consuming calculation (let say runs a for loop
for 5000000 times) then the handler of the second
listener will not be called unless the handler of the
first listener returns.

I have tested it by a couple of techniques.

1. Handler function of first listener displays an
alert and then iterates for 5000000 times (performing
some useless tasks like modifying the same property of
the object). Handler function of second listener
simply display alert. Both alert message boxes are
displyed at the same time even though the first
handler is still inside the for loop.

2. Here I apply the break point on the first
instruction of handler functions of both listeners.
The functionaly of handler functions are the same as
expained in above. The debugger break on the
instruction of first handler and then I press F8. The
debugger then breaks on the instruction of the second
handler, however, the first handler function is still
inside the for loop.

Can you still confirm that eventdispatcher invokes the
handlers in synchronous manner? I am implementing a
publisher-subscriber scenario using EventDispatcher
and the desing decision will be highly affected by the
behavior of deafault EventDispatcher.
--- Gordon Smith <[EMAIL PROTECTED] <mailto:gosmith%40adobe.com> >
wrote:

> > Does EventDispatcher calls the listeners
> synchronously or
> asynchronously?
> 
> Synchrously, in the order that they were added. To
> see that the handlers
> execute before dispatchEvent() returns, add a
> handler which traces out
> something and single-step in FlexBuilder or fdb over
> a dispatchEvent()
> call. You should see the trace output.
> 
> BTW, I may be the only person in the world that
> distinguishes between a
> listener and a handler. A listener is an object that
> is interested in
> knowing about an event dispatched by another object,
> while a handler is
> a method of the listener that executes in response
> to the event. The
> name addEventListener() is somewhat confusing. You
> might think that
> you'd have to pass it both listener and a handler
> (to tell it which
> method to execute, and which object to use as the
> method's 'this'), but
> due to the magic of method closures, you only need
> to pass the handler
> and it implies the listener.
> 
> > Does the EventDispatcher make copies of the event
> > object provided to EventDispatcher's dispatchEvent
> > function when calling the listeners? Let say, I
> define
> > 2 listeners for specific event type. The first
> > listener modify some properties of the event
> object.
> > Do the second listener get the modified one or the
> > copy of the original one?
> 
> The second listener gets the modified event
> instance. A copy doesn't
> happen in this case.
> 
> However, when an event handler calls dispatchEvent()
> to redispatch the
> event in the middle of event processing, then
> dispatchEvent() close the
> event's clone() method to copy it and redispatches
> the copy.
> 
> Gordon Smith
> Adobe Flex SDK Team
> 
> ________________________________
> 
> From: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>

> [mailto:flexcoders@yahoogroups.com
<mailto:flexcoders%40yahoogroups.com> ] On
> Behalf Of Jehanzeb Musani
> Sent: Wednesday, November 28, 2007 1:42 PM
> To: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com> 
> Subject: RE: [flexcoders] Events and Non-Display
> Objects
> 
> 
> 
> You can adopt the following two options to achieve
> your requirements.
> 
> 1. Your MXML Object should Implement the
> IEventDispatcher interface and internally it creates
> an instance of EventDispathcer class and delegates
> calls to that instance.
> 
> 2. If you dont want to use built-in event dispatcher
> and you want to define your own function definition,
> then you can defines an interface and all the
> listeners should implement that interface. Moreover,
> you should methods in your MXML Object to register
> and
> unregister those listeners and call the specific
> method of that interface on some condition.
> 
> Gordon, I want to ask you a few questions regarding
> event dispatching mechanism of default
> EventDispatcher.
> 
> 1. Does EventDispatcher calls the listeners
> synchronously or asynchronously?
> 
> 2. Does the EventDispatcher make copies of the event
> object provided to EventDispatcher's dispatchEvent
> function when calling the listeners? Let say, I
> define
> 2 listeners for specific event type. The first
> listener modify some properties of the event object.
> Do the second listener get the modified one or the
> copy of the original one?
> 
> --- Gordon Smith <[EMAIL PROTECTED] <mailto:gosmith%40adobe.com> 
> <mailto:gosmith%40adobe.com> >
> wrote:
> 
> > Or your Object subclass could create an
> > EventDispatcher instance which
> > can dispatch events on its behalf:
> > 
> > var eventDispatcher:EventDispatcher = new
> > EventDispatcher(this);
> > eventDispatcher.dispatchEvent(event);
> > 
> > Gordon Smith
> > Adobe Flex SDK Team
> > 
> > ________________________________
> > 
> > From: flexcoders@yahoogroups.com
<mailto:flexcoders%40yahoogroups.com> 
> <mailto:flexcoders%40yahoogroups.com>
> 
> > [mailto:flexcoders@yahoogroups.com
<mailto:flexcoders%40yahoogroups.com> 
> <mailto:flexcoders%40yahoogroups.com> ] On
> > Behalf Of Alex Harui
> > Sent: Wednesday, November 28, 2007 1:26 PM
> > To: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>

> <mailto:flexcoders%40yahoogroups.com> 
> > Subject: RE: [flexcoders] Events and Non-Display
> > Objects
> > 
> > 
> > 
> > You can try extending EventDispatcher and dispatch
> > events
> > 
> > ________________________________
> > 
> > From: flexcoders@yahoogroups.com
<mailto:flexcoders%40yahoogroups.com> 
> <mailto:flexcoders%40yahoogroups.com>
> 
> > [mailto:flexcoders@yahoogroups.com
<mailto:flexcoders%40yahoogroups.com> 
> <mailto:flexcoders%40yahoogroups.com> ] On
> > Behalf Of bithroop
> > Sent: Wednesday, November 28, 2007 10:18 AM
> > To: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>

> <mailto:flexcoders%40yahoogroups.com> 
> > Subject: [flexcoders] Events and Non-Display
> Objects
> > 
> > 
> > 
> > General question here...
> > 
> > I understand Event flow with objects in the
> display
> > list. They flow
> > either up (bubbling) or down (capture) between
> > parent and child. Maybe
> > an oversimplification but my question really is
> > about how to handle
> > events with objects that are not in the display
> > list. How do you pass
> > events from them? Like for instance, I've got this
> > situation here...
> > 
> > I have an MXML that is just an <mx:Object>. It's
> > MXML because I'm
> > defining some remoteObjects in it and that's just
> > way more fun to do
> > in MXML than in AS. So anyway, regardless, the
> > methods inside are
> > static and I don't want to draw the thing. It just
> > needs to hang out
> > and be available so I can make these remoting
> calls.
> > 
> > So there's the rub. How can this MXML talk back to
> > where it was called
> > from? Can I use events for this or do I need to
> send
> > a callback
> > function?
> > 
> > Any wisdom here would be awesome... this has been
> > befuddling me for a
> > while.
> > 
> > -b
> > 
> > 
> > 
> > 
> > 
> 
>
__________________________________________________________
> Get easy, one-click access to your favorites. 
> Make Yahoo! your homepage.
> http://www.yahoo.com/r/hs <http://www.yahoo.com/r/hs> 
> <http://www.yahoo.com/r/hs <http://www.yahoo.com/r/hs> > 
> 
> 
> 
> 
=== message truncated ===

__________________________________________________________
Never miss a thing. Make Yahoo your home page. 
http://www.yahoo.com/r/hs <http://www.yahoo.com/r/hs> 


 

Reply via email to