Flex 等效于 ProcessMessages 和长循环期间无响应的 UI [英] Flex equivalent of ProcessMessages and unresponsive UI during long loops

查看:20
本文介绍了Flex 等效于 ProcessMessages 和长循环期间无响应的 UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我的 Flex 应用程序的 UI 在很长的处理循环(数十秒)中变得无响应.例如,在处理非常大的 XML 文件并为每个元素做一些事情时......

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...

是否有等效的ProcessMessages"?也就是说,一个调用会告诉 Flex 继续响应 UI 事件,即使在某个长循环的中间,以便 UI 不会变得无响应?

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?

我知道 Flex 是单线程设计的.这正是我要寻找类似 ProcessMessages() 之类的东西的原因——该函数允许单线程可重入应用程序(如在 VB 中,或基于单线程消息循环的 C++ 应用程序)在长时间内保持响应操作.

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.

答案摘要

  1. Flex 中没有像 HandleEvents()ProcessMessages() 这样的内置函数.
  2. 使用某种回调机制来迭代处理长计算过程的块,同时让步于块之间的运行时,从而使其具有响应性,这是在长计算期间保持响应式 UI 的唯一方法.
  3. 实现上述目标的方法有:
  1. There's no built-in function like HandleEvents() or ProcessMessages() in Flex.
  2. 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.
  3. Ways of accomplishing the above are:
  1. 使用 enterFrame 事件,每当 Flex 应用程序下方的 Flash电影"层刷新其帧(类似于 20fps)时都会调用该事件.
  2. 使用计时器.
  3. 使用 UIComponent.callLater() 安排工作稍后"完成.(如文档:将一个函数排入队列以供稍后调用.在每次更新屏幕之前,Flash Player 或 AIR 调用为更新安排的一组函数.
  4. 使用有意触发的鼠标/键盘事件创建伪工作线程",如这个例子.
  1. Using the enterFrame event, which is called whenever the Flash "movie" layer below the Flex application refreshes its frame (which is something like 20fps).
  2. Using a timer.
  3. 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.
  4. Using intentionally triggered mouse/keyboard events to create pseudo "worker threads", as in this example.

如果有进一步的建议,或者我遗漏了什么,请随时编辑这个(现在)维基文章.

If there are further suggestions, or if I left out anything, please feel free to edit this (now) wiki piece.

推荐答案

问题在于 Flash 是单线程的,即在部分代码运行之前,无法进行其他处理.您需要以某种方式将处理分解为更小的块并执行这些块,例如,在 enterFrame 事件上.

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.

编辑:恐怕对这个(或西蒙的)答案投反对票并不会改变这在 AS3 中不可行的事实.阅读这篇文章,了解有关该问题的更多信息.本文还包括一个名为 PseudoThread 的简单库",它有助于执行长时间的后台计算.不过,您仍然需要自己将问题分解成更小的部分.

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.

这篇关于Flex 等效于 ProcessMessages 和长循环期间无响应的 UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆