I find that my Flex application's UI becomes unresponsive during very long processing loops (tens of seconds). For example, while processing very large XML files and doing something per-element...
Is there an equivalent of "ProcessMessages"? That is, a call that would tell Flex to continue responding to UI events, even in the middle of some long loop, so that the UI doesn't become unresponsive?
I'm aware Flex is single threaded by design. That's exactly why I'm looking for something like ProcessMessages() - a function that allows single-threaded reentrant applications (like in VB, or single-threaded message loop based C++ applications) to remain responsive during long operations.
Summary of Answers
- There's no built-in function like
HandleEvents()
orProcessMessages()
in Flex. - Using some sort of callback mechanism to iteratively process chunks of a long computation process, while yielding to the runtime between chunks, thus enabling it to be responsive, is the only way to maintain a responsive UI during long computations.
- Ways of accomplishing the above are:
- Using the
enterFrame
event, which is called whenever the Flash "movie" layer below the Flex application refreshes its frame (which is something like 20fps). - Using a timer.
- Using
UIComponent.callLater()
which schedules work to be done "later". (as the docs say:Queues a function to be called later. Before each update of the screen, Flash Player or AIR calls the set of functions that are scheduled for the update.
- Using intentionally triggered mouse/keyboard events to create pseudo "worker threads", as in this example.
- Using the
If there are further suggestions, or if I left out anything, please feel free to edit this (now) wiki piece.