Flex equivalent of ProcessMessages and unresponsive UI during long loops

前端 未结 5 2257
陌清茗
陌清茗 2020-12-17 19:00

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

相关标签:
5条回答
  • 2020-12-17 19:29

    The process model for ActionScript is single threaded, so the answer is no. Best bet is to either defer to an asynchronous task on the server if you can, or pop up a wait cursor while your long loop runs, or break your process into some smaller pieces which are not quite as intrusive to the UI, or perform the long running tasks at a moment when the user is less likely to feel the effect (app startup for instance).

    0 讨论(0)
  • 2020-12-17 19:31

    The problem is that Flash is single threaded, i.e. until a part of the code is running, no other processing can be made. You'll somehow need to break up the processing into smaller chunks and execute these chunks, say, on the enterFrame event.

    Edit: I'm afraid that downvoting this (or Simon's) answer does not change the fact that this is not doable in AS3. Read this article for more insight on the problem. The article also includes a simple "library" called PseudoThread, which helps in executing long background computations. You still have to break up the problem into smaller pieces yourself, though.

    0 讨论(0)
  • 2020-12-17 19:42

    There is no equivalent functionality in Flash Player. By design, Flash Player alternates between rendering to the screen and then running all of the code for each frame. Using Event.ENTER_FRAME events on display objects, or Timer objects elsewhere, are the best bet for breaking up very long calculations.

    It's worth noting that some events in ActionScript have an updateAfterEvent() function with the following description:

    Instructs Flash Player or the AIR runtime to render after processing of this event completes, if the display list has been modified.

    In particular, TimerEvent, MouseEvent, and KeyboardEvent support updateAfterEvent(). There may be others, but those are the ones I found with a quick search.

    0 讨论(0)
  • 2020-12-17 19:47

    Actionscript is single threaded by design, no amount of downvoting answers will change that.

    For compatibility your best bet is to try to split up your processing into smaller chunks, and do your processing iteratively.

    If you absolutely need threading it can sort of be done in Flash Player 10 using Pixel Bender filters. These will run on a separate thread and can give you a callback once they are done.
    I believe they are well suited for "hardcore" processing tasks, so they might fit your purpose nicely. However, they will put a whole other set of demands on your code, so you might be better of doing small "buckets" of computing anyways.

    0 讨论(0)
  • 2020-12-17 19:52

    I can tell you definitively that as of Flex 3, there is no built-in construct similar to the ProcessMessages functionality you are describing.

    The most common way to work around this is to split whatever work you are processing into a queue of "worker" tasks and a "worker manager" to control the queue. As each "worker" completes its processing, the worker queue manager pops the next worker off the queue and executes it in a callLater() invocation. This will have an effect that is similar to "yielding to the main thread" and allow your UI to receive events and be responsive, while still allowing the processing to continue when control is returned to the worker.

    Once you've got this working, you can do some testing and profiling to figure out if your application can get away with executing a run of multiple workers before invoking callLater(), and encapsulate this logic within the worker queue manager. For example, in our implementation of this pattern (with a few hundred workers in the queue), we were able to get the processing done more quickly with a comparable perceived performance by executing 4 workers before "yielding to the main thread" with callLater(), but this is totally dependent on the scope and nature of the work that your workers are doing.

    0 讨论(0)
提交回复
热议问题