java - 在新线程中打开 Eclipse Wizard 或 Eclipse MessageDialog

标签 java eclipse multithreading

我想在新线程中打开 Eclipse WizardMessageDialog,但不知怎的,我总是遇到这样的异常:

Exception in thread "Thread-7" org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:4491)
    at org.eclipse.swt.SWT.error(SWT.java:4406)
    at org.eclipse.swt.SWT.error(SWT.java:4377)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:482)
    at org.eclipse.swt.widgets.Shell.<init>(Shell.java:266)
    at org.eclipse.swt.widgets.Shell.<init>(Shell.java:362)
    at org.eclipse.jface.window.Window.createShell(Window.java:486)
    at org.eclipse.jface.window.Window.create(Window.java:429)
    at org.eclipse.jface.dialogs.Dialog.create(Dialog.java:1096)
    at org.eclipse.jface.window.Window.open(Window.java:792)
    at org.eclipse.jface.dialogs.MessageDialog.open(MessageDialog.java:330)
    at de.uka.ipd.sdq.beagle.gui.GuiController$DialogPolling.run(GuiController.java:126)
    at java.lang.Thread.run(Thread.java:745)

当使用这样的代码时:

/**
 * Opens up the dialog displaying the actions "pause", "continue", and "abort" to the
 * user. These actions are regarding the analysis.
 */
private void engageDialog() {
    final String dialogTitle = "Beagle Analysis is Running";
    final String dialogMessage = "Beagle Analysis is running.";
    final String[] buttonLabels = {"Abort", "Pause"};
    this.messageDialog =
        new MessageDialog(this.shell, dialogTitle, null, dialogMessage, MessageDialog.INFORMATION, buttonLabels, 0);
    new Thread(new DialogPolling()).start();
}

private class DialogPolling implements Runnable {

    @Override
    public void run() {
        final int buttonClick = GuiController.this.messageDialog.open(); // line 126

        if (buttonClick == 0) {
            System.out.println("User clicked 'Abort'.");
        }

        if (buttonClick == 1) {
            System.out.println("User clicked 'Pause'.");
        }
    }
}

这是来自 GuiController 并标记了第 126 行。如果看不到行号,请向右滚动。

如何在新线程中打开向导或消息对话框?

最佳答案

所有向导、对话框...都必须在单个 SWT UI 线程中打开。您可以在另一个线程中使用 Display.syncExec 调用来运行在 UI 线程中打开的对话框。

Display.getDefault().syncExec(runnable);

您的Runnable可以调用对话框open并将buttonClick值保存在您可以在syncExec返回时访问的地方。

关于java - 在新线程中打开 Eclipse Wizard 或 Eclipse MessageDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34800728/

相关文章:

java - OnOptionsItemSelected - 按一个按钮执行另一个按钮的操作

java - MvC模式在eclipse luna IDE中的工作流程

java - Spring MVC 和 PDF

c++ - 其他线程位置

Java 使用的内存多于分配的内存

java - 枚举中的枚举

java - scala 计算读取文件中包含的列表中的字符串

android - 设备未显示在 DDMS 中

java - java jar文件的mysql连接器

node.js - Node 杀死线程但不杀死进程