简体   繁体   English

在 SWT eclipse 插件上更新用户界面的最佳技术

[英]Best technique to update user interface on SWT eclipse plugin

I'm developing a plugin for my own projects (Eclipse foundation, using SWT), but I'm faced with the following problem.我正在为我自己的项目(Eclipse 基础,使用 SWT)开发一个插件,但我面临以下问题。 The plugin does a lot of work when the execution starts, so it can take 2 or 3 minutes to finish the whole operation.该插件在执行开始时会做很多工作,因此完成整个操作可能需要 2 到 3 分钟。 I decided to implement a log window so I can check out the progress and any error it may happen.我决定实现一个日志窗口,以便我可以查看进度和可能发生的任何错误。 So while it does his job, the window/frame is filled with all the info when the whole operation is finished.因此,当它完成他的工作时,当整个操作完成时,窗口/框架会充满所有信息。 Naturally I figured that since its using a single thread to do everything, the "refresh interface" event is triggered once the thread ends the operation.我自然地认为,由于它使用单个线程来完成所有操作,因此一旦线程结束操作,就会触发“刷新界面”事件。

Is there any proper or "correct" way to solve this problem?是否有任何适当或“正确”的方法来解决这个问题? I'm accostumed to web application development, and I have very little knowledge of SWT in general.我已经习惯了 Web 应用程序开发,而且我对 SWT 的一般知识知之甚少。 I have an idea of using different threads to handle the different operations, synchronizing them when its needed, but I'm not completely sure if that would work or if there is a better approach within the boundaries of standalone apps.我有一个想法,使用不同的线程来处理不同的操作,在需要时同步它们,但我不完全确定这是否可行,或者在独立应用程序的边界内是否有更好的方法。

Thanks for any tip or hint!感谢您的任何提示或提示!

So at the end I followed the suggestions, I generated a custom Job that uses the Display.getDefault().asyncExec() operation.所以最后我遵循了建议,我生成了一个使用 Display.getDefault().asyncExec() 操作的自定义作业。 A small sample like this worked:像这样的小样本有效:

Job synJob = Job.create("Synchronize Files", (ICoreRunnable) monitor -> {

for(String filePath : paths) {
    //my operation that sync some files in workspace in a loop ...
    //...
    //now log some data, updating the UI
    Display.getDefault().asyncExec(() -> {
    //generates a text and appends it to a frame etc.
    Label label = new Label(myFrame, 0);
    //etc
    });
 }

  });

  synJob.schedule();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM