gpt4 book ai didi

java - 组织.eclipse.swt.SWTException : Invalid thread access

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:20 25 4
gpt4 key购买 nike

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

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 方法时没有任何反应。我可以在不必创建另一个方法的情况下对这个无效线程访问进行排序吗??

最佳答案

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

简而言之,没有。

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

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

关于java - 组织.eclipse.swt.SWTException : Invalid thread access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22654031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com