日期:2014-05-20  浏览次数:20805 次

关于SWT线程错误的问题
我这里做了一个用swt镶嵌url连接地址的小程序,但是不知道为什么偶尔在打开新窗口时会报错,并且打不开新窗口,请高手来帮我看看代码。
Java code
public Window(final Display display) {
        shell = new Shell(display);
        display.asyncExec(new Runnable() {
            public void run() {
                Shell shellBrowser = new Shell(display);
                shellBrowser.setText("技术监督服务平台");
                
                shellBrowser.setLocation(Display.getCurrent().getClientArea().width / 2 - shellBrowser.getShell().getSize().x/2, Display.getCurrent() 
                        .getClientArea().height / 2 - shellBrowser.getSize().y/2);
                shellBrowser.setMaximized(true);
                shellBrowser.setLayout(new FillLayout());
                Browser browser = new Browser(shellBrowser, SWT.NONE);
                BrowserTest1.initialize(display, browser);
                shellBrowser.open();
                browser.setUrl("http://localhost:8089/tssp/index.jsp?security=madf823sdfasd09213");
                while (!shellBrowser.isDisposed()) {
                    if (!display.readAndDispatch())
                        display.sleep();
                }
                display.dispose();
            }

        });

        
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

    }



错误代码
Java code
Caused by: org.eclipse.swt.SWTException: Failed to execute runnable (org.eclipse.swt.SWTException: Widget is disposed)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Unknown Source)
    at org.eclipse.swt.widgets.Display.runAsyncMessages(Unknown Source)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
    at com.sihan.epis.client.Window$1.run(Window.java:35)
    at org.eclipse.swt.widgets.RunnableLock.run(Unknown Source)
    ... 5 more
Caused by: org.eclipse.swt.SWTException: Widget is disposed
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.widgets.Widget.error(Unknown Source)
    at org.eclipse.swt.widgets.Widget.checkWidget(Unknown Source)
    at org.eclipse.swt.browser.Browser.checkWidget(Unknown Source)
    at org.eclipse.swt.browser.Browser.getUrl(Unknown Source)
    at com.sihan.epis.client.BrowserTest1$2$1.run(BrowserTest1.java:64)
    at org.eclipse.swt.widgets.RunnableLock.run(Unknown Source)
    ... 10 more


------解决方案--------------------
不知道你要搞什么,为什么外面有个shell了,又new了一个。
这个也不是线程错误,你里面把display给dispose了,这个就是错误原因。

------解决方案--------------------
你的第一个shell还没有open操作就
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
 }
这怎么会打得开?