插件开发:测试插件时 Eclipse 挂起

Plugin Development: Eclipse hangs when testing plugin

我是开发插件的新手,想知道是什么原因导致测试插件在启动时挂起,即 Eclipse 没有响应。 我知道我的代码正在运行,因为我开发了一个语音识别插件来将所说的内容写入屏幕,当我打开记事本时,我所说的一切都会打印到记事本中。

所以我想知道,我是否在插件生命周期中遗漏了某些导致 IDE 在我的插件启动时挂起的东西?

package recognise.handlers;

public class SampleHandler extends AbstractHandler {

    public SampleHandler() {
    }

    /**
     * the command has been executed, so extract extract the needed information
     * from the application context.
     */
    public Object execute(ExecutionEvent event) throws ExecutionException {
        boolean finish = false;
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        MessageDialog.openInformation(
                window.getShell(),
                "Recognise",
                "Starting Recognition");
        TakeInput start = new TakeInput();
        //Stage a = new Stage();
        //SceneManager scene = new SceneManager();
        try {
            start.startVoiceRecognition(finish);
            //scene.start(a);
        } catch (IOException | AWTException e) {
            e.printStackTrace();
        }

        return null;
    }
}

start.startVoiceRecognition() 是否需要线程化?

提前致谢,如果您想看我的 manifest/activator 等,请告诉我

结论

向 UI 线程添加了一个单独的作业

/*
 * Start a new job separate to the main thread so the UI will not
 * become unresponsive when the plugin has started
 */
  public void runVoiceRecognitionJob() {
    Job job = new Job("Voice Recognition Job") {
      @Override
      protected IStatus run(IProgressMonitor monitor) {
     TakeInput start = new TakeInput();
     try {
        start.startVoiceRecognition(true);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        // use this to open a Shell in the UI thread
        return Status.OK_STATUS;
      }

    };
    job.setUser(true);
    job.schedule();
  }

如图所示start.startVoiceRecognition()在UI线程中是运行,它会阻塞UI线程直到它完成,在此期间应用程序将无响应时间。因此,如果它正在做大量工作,请使用 Thread 或使用 Eclipse Job(它在 Eclipse 管理的后台线程中运行)。

要解锁您的 UI,您必须使用显示线程。

 /**
         * the command has been executed, so extract extract the needed information
         * from the application context.
         */
        public Object execute(ExecutionEvent event) throws ExecutionException {

Display.getDefault().asyncExec(new Runnable() {
      public void run() {

            boolean finish = false;
            IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
            MessageDialog.openInformation(
                    window.getShell(),
                    "Recognise",
                    "Starting Recognition");
            TakeInput start = new TakeInput();
            //Stage a = new Stage();
            //SceneManager scene = new SceneManager();
            try {
                start.startVoiceRecognition(finish);
                //scene.start(a);
            } catch (IOException | AWTException e) {
                e.printStackTrace();
            }
     MessageDialog.openInformation(shell, "Your Popup ",
            "Your job has finished.");
      }
    });
            return null;
        }

您可以使用 Display.getDefault().asyncExec() 如上所述,因此您的 UI 将被解锁,而您的非 UI 代码将被执行。