Question

I am Opening outlook and attaching a file using My Java Web Application (JSF). The Methd is working Only once and when I attempt to call the Method again I get:

org.eclipse.swt.SWTException: Invalid thread access

Here is My method:

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" */);
    }

After Looking around I found out that I should create a method like this:

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

Which I have but when I call the exec Method Nothing Happens. can I sort this Invalid thread access without Neccesarily having to create another Method??

Was it helpful?

Solution

can I sort this Invalid thread access without Neccesarily having to create another Method?

In short, no.

In long: Anything that changes or adds something to the GUI in SWT needs to be called on the Display Thread, otherwise an Invalid thread access Exception will be thrown. You can either call asyncExec(Runnable) or syncExec(Runnable r)

Now, by looking at this question, I think that syncExec might be a better option for you.

OTHER TIPS

There is a solution for this :

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
}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top