我是开发插件的新手,并且想知道是什么原因导致测试插件在启动时挂起,即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()是否需要进行线程化?
提前致谢,如果您想查看我的清单/激活器等,请告诉我。
结论
添加了与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();
}
答案 0 :(得分:0)
如图所示,start.startVoiceRecognition()
正在UI线程中运行,它将阻止UI线程,直到它完成并且应用程序在此期间无响应。因此,如果它正在进行大量工作,请使用Thread
或使用Eclipse Job
(在Eclipse管理的后台线程中运行)。
答案 1 :(得分:0)
要取消阻止您的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代码将会执行。