【问题标题】:org.eclipse.swt.SWTException: Invalid thread accessorg.eclipse.swt.SWTException:无效的线程访问
【发布时间】:2014-03-26 07:17:43
【问题描述】:

我正在使用我的 Java Web 应用程序 (JSF) 打开 Outlook 并附加一个文件。方法只工作一次,当我再次尝试调用方法时,我得到:

org.eclipse.swt.SWTException: Invalid thread access

这是我的方法:

public void sendemails() {
        Display display = Display.getCurrent();
        Shell shell = new Shell(display);//Error is Happening on this Line.
        OleFrame frame = new OleFrame(shell, SWT.NONE);
        // This should start outlook if it is not running yet
        OleClientSite site = new OleClientSite(frame, SWT.NONE, "OVCtl.OVCtl");
        site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
        // Now get the outlook application
        OleClientSite site2 = new OleClientSite(frame, SWT.NONE, "Outlook.Application");
        OleAutomation outlook = new OleAutomation(site2);
        OleAutomation mail = invoke(outlook, "CreateItem", 0 /* Mail item */).getAutomation();
        setProperty(mail, "BodyFormat", 2 /* HTML */);
        setProperty(mail, "Subject", "Testing");
        setProperty(mail, "HtmlBody", "I am Sending to you");
        ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        String path = (String) ctx.getAttribute("capturepath");
        File file = new File(path);

        if (file.exists()) {
            OleAutomation attachments = getProperty(mail, "Attachments");
            invoke(attachments, "Add", path);
        } else {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_INFO,
                            "Failed!",
                            "File " + path + " Does Not exists"));
        }
        invoke(mail, "Display" /* or "Send" */);
    }

环顾四周后,我发现我应该创建一个这样的方法:

public void exec() {
        Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {
                sendemails();
                System.out.println("Outlook called");
            }
        });
    }

我有,但是当我调用 exec 方法时,什么都没有发生。我可以在不必创建另一个方法的情况下对这个无效的线程访问进行排序吗?

【问题讨论】:

    标签: java eclipse exception swt


    【解决方案1】:

    我可以对这个无效的线程访问进行排序而不必 创建另一个方法?

    简而言之,没有。

    长篇大论: 在 SWT 中任何改变或添加到 GUI 的东西都需要在 Display Thread 上调用,否则将抛出 Invalid thread access Exception。您可以致电asyncExec(Runnable)syncExec(Runnable r)

    现在,通过查看 this question,我认为 syncExec 可能是您更好的选择。

    【讨论】:

    • 感谢您的建议,但这只会调用 Outlook 一次,以后什么都没有发生,没有错误 .. 注意:(
    • 不,只是没有响应。
    • 那么,错误是否停止显示在代码中的标记行?请您编辑问题,我再看看。
    【解决方案2】:

    有一个解决方案:

    Display.getDefault().syncExec(new Runnable(){
    public void run() {
        // Your code to run Synchronously
    }
    });
    
    
    Display.getDefault().asyncExec(new Runnable(){
    public void run() {
        // // Your code to run Asynchronously
    }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2015-04-24
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多