3 Replies Latest reply on Jun 15, 2009 3:31 PM by Flex harUI

    Any resources on how the event queue works?

    MackrorySD

      I'm trying to find more details on how the event queue is handled in Flex. I understand that Flex is single-threaded, but the event queue is clearly not 'linear'. My original assumption (based on Flex being single threaded, and the use of the term 'event queue') was that each item in the event queue was processed from beginning to end in the order it was pushed on to the queue. However - an actual practical test shows that this is not the case.

       

      In reality, Flex jumps around quite a lot and seems to have a scheduling mechanism that prioritizes items in the event queue. I would assume that this mechanism can also place items on hold once their execution has begun, so that they can be finished later. Does anyone know of any resources for learning more about this? I've googled like crazy for several weeks, waded through several hours worth of "Flash Internals" presentations, and the only answers I've found are confirmations that "Flash is single-threaded" and that something called an event queue exists. Beyond that, I've been left to write my own experiment apps, but the results are inconsistent. I really don't know where else I can look... Any thoughts?

        • 1. Re: Any resources on how the event queue works?
          Flex harUI Adobe Employee

          There is no event queue in actionscript.  You call dispatchEvent and the line after it will not run until all other handlers for that event and any events those handlers dispatch are handled.

           

          There is an input event queue for mouse, keyboard and timer events only in the sense that, if you are running lots of other code when you click the mouse, the mouse/keyboard/timer event will not fire until after that code finishes.  You can probably get a few events backed up if you're running lots of code, but that will make your app appear unresponsive.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          1 person found this helpful
          • 2. Re: Any resources on how the event queue works?
            MackrorySD Level 1

            So let's say I have an event handler that is called in response to a mouse click. That event handler dispatches several other events, and triggers a few bindings before executing some additional code. So at some point, the virtual machine now needs to

             

            1) Handle all of the bindings that have been triggered (either calling the appropriate function, or assigning the appropriate value)

             

            2) Handle the other events that have now been triggered

             

            3) Finish handling the current event

             

            So what is the mechanism by which the virtual machine keeps track of all this code that now needs to be executed at some point, and how does it determine what order things need to happen in? Does it ever switch between tasks before completely finishing one of them?

            • 3. Re: Any resources on how the event queue works?
              Flex harUI Adobe Employee

              If you think of event dispatching and bindings as function calls (which they are) then it is easier to see that the order is completely deterministic by the order in which the app was initialized and bindings were set up and other listeners added.

               

              There is no switching of tasks.  If you don't get all done quickly enough, you'll get a script timeout, but long before that, your app will appear sluggish as the next mouse event can't get processed until all of the other code has run.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui