将 Dispatch 与 SWT 主循环结合使用
我的 java / jython 应用程序运行一个“默认”SWT 主循环,如下所示:
while not shell.isDisposed():
if not display.readAndDispatch():
display.sleep()
在 Mac OS X 上,所有与 SWT 相关的代码都需要在主线程中运行,如下所示:
from com.apple.concurrent import Dispatch
call = Dispatch.getInstance().getNonBlockingMainQueueExecutor().execute
class Main(Runnable):
def run(self):
# main loop here
call(Main())
到目前为止,效果很好。现在到有问题的部分:我需要在某个时间点从 Main.run
方法外部执行与 SWT 相关的代码。
当我在主循环运行时使用相同的 call()
魔法时,由于线程繁忙(上面所示的异步调用立即返回,但从不执行任何代码,并且同步调用阻塞),所以什么也没有发生永远)。
display.readAndDispatch()
不应该实际处理这些调用吗?有什么方法可以以干净的方式在此线程上执行代码(即,无需构建自己的事件队列)?
在主循环内重新实现这些东西相对容易,但我觉得我在这里遗漏了一些非常明显的东西。
My java / jython app runs a ‘default’ SWT main loop like this:
while not shell.isDisposed():
if not display.readAndDispatch():
display.sleep()
On Mac OS X, all SWT-related code needs to run in the main thread, as follows:
from com.apple.concurrent import Dispatch
call = Dispatch.getInstance().getNonBlockingMainQueueExecutor().execute
class Main(Runnable):
def run(self):
# main loop here
call(Main())
So far this works fine. Now to the problematic part: I need, at some point in time, execute SWT-related code from outside of the Main.run
method.
When I use the same call()
magic while the main loop is running, nothing happens since the thread is busy (the async call as shown above returns immediately, but never executes any code, and synchronous call blocks forever).
Shouldn’t display.readAndDispatch()
actually process these calls? Is there any way I can execute code on this thread in clean fashion (that is, without building an event queue of my own)?
Reimplementing this stuff inside of the main loop would be relatively easy, but I feel like I’m missing something very obvious here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
弄清楚了这一点,
display.asyncExec
就做到了这一点。Figured this out,
display.asyncExec
does just that.