你怎么能在这里获得"SWTException:无效的线程访问"?

Cra*_*lus 1 java eclipse swt jface eclipse-rcp

我正在注册一个生命周期监听器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);  
        }
     });
Run Code Online (Sandbox Code Playgroud)

但如果我改变呼叫也使用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  
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

org.eclipse.e4.core.di.InjectionException:org.eclipse.swt.SWTException:在org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:63)无效的线程访问在
org.eclipse位于org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:206)的.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:229)at

我知道这个异常必须对UI线程做一些事情,但我无法弄清楚如何使用Display导致此异常.
有帮助吗?

Den*_*ret 5

使用Display.asyncExec方法在UI线程上执行Runnable.

与大多数UI框架一样,SWT不允许任何线程对UI组件起作用,因此当您从另一个线程更改某些内容时,必须将其提供给在UI线程上执行它的此实用程序.