我正在plugin.xml
中注册生命周期侦听器。如果我只定义一个Shell
,它可以正常运行.
例如
@PostContextCreate
void postContextCreate(final IEventBroker eventBroker){
System.out.println("CALLED!");
final Shell shell = new Shell(SWT.TOOL | SWT.NO_TRIM);
shell.open();
eventBroker.subscribe(UIEvents.UILifeCycle.ACTIVATE, new EventHandler() {
@Override
public void handleEvent(Event event) {
System.out.println("Closing shell");
shell.close();
shell.dispose();
System.out.println("Closed");
eventBroker.unsubscribe(this);
}
});
但是如果我将调用更改为也使用Display
:
@PostContextCreate
void postContextCreate(final IEventBroker eventBroker){
System.out.println("CALLED!");
Display display = new Display();
final Shell shell = createSplashShell(display);
shell.open();
while (!shell.isDisposed ()) {
if (!display .readAndDispatch ()) display.sleep ();
}
display.dispose ();
//etc
我得到以下异常:
org.eclipse.e4.core.di.InjectionException: org.eclipse.swt.SWTException:无效的线程访问 org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:63) 在
org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:229) 在 org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:206) 在
我知道这个异常必须与UI
线程做一些事情,但我无法弄清楚如何使用Display
在这里导致此异常。
有什么帮助吗?
使用 Display.asyncExec 方法在 UI 线程上执行 Runnable。
像大多数UI框架一样,SWT不允许任何线程作用于UI组件,因此当您要从另一个线程更改某些内容时,必须将其提供给此实用程序,该实用程序在UI线程上执行它。
根据我的理解,我看到您想在事件发生时弹出一个 shell。一个显示器(由主RCP应用程序创建)足以构建新的shell和处理事件。
@PostContextCreate
void postContextCreate(final IEventBroker eventBroker){
System.out.println("CALLED!");
final Display display = Display.getDefault();
Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
final Shell shell = createSplashShell(display);
shell.open();
while (!shell.isDisposed ()) {
if (!display .readAndDispatch ()) display.sleep ();
}
}
}