最近在学习swt的东西,遇到一个问题,特转录如下。
SWT异常: org.eclipse.swt.SWTException: Invalid thread access
在创建SWT界面的线程之外的线程中尝试去修改界面元素.将抛出以下异常
Exceptioninthread"Thread-0"org.eclipse.swt.SWTException:Invalidthreadaccess
atorg.eclipse.swt.SWT.error(SWT.java:2942)
atorg.eclipse.swt.SWT.error(SWT.java:2865)
atorg.eclipse.swt.SWT.error(SWT.java:2836)
上述Thread-0是另外开启的一个线程.
【解析】:
在SWT程序中,
SWT会自动创建一个用户界面线程
非用户界面线程不能直接操作用户界面线程
要想在另外一个线程中尝试修改用户界面,应采用一下方法:

if(!this.display.isDisposed())...{

Runnablerunnable=newRunnable()...{

publicvoidrun()...{
//你改界面的代码
}
};
display.syncExec(runnable);//关键在这一句上(同步调用,等待主界面线程处理完成之后)
}
swt-doc中的说明:
public void syncExec(java.lang.Runnable runnable)Causes the run() method of the runnable to be invoked by the user-interface thread at the next reasonable opportunity. The thread which calls this method is suspended until the runnable completes.
Parameters:
runnable - code to run on the user-interface thread. (同步调用,需要等待主界面处理完成之后,才能继续)
此外,与之对应的另一个方法:
public void asyncExec(java.lang.Runnable runnable)Causes the run() method of the runnable to be invoked by the user-interface thread at the next reasonable opportunity. The caller of this method continues to run in parallel, and is not notified when the runnable has completed.
Parameters:
runnable - code to run on the user-interface thread. (异步调用,不等待主界面线程处理结果)
分享到:
相关推荐
修复了MqttAndroidClient会出现throw new IllegalArgumentException Invalid ClientHandle报错,而源github中代码未更新解决,所以经过重新编译,生成新的jar包,增加IllegalArgumentException异常处理,替换关联的...
找到eclipse安装目录的features,将其目录中的org.sonatype.m2e.mavenarchiver.feature_0.NNN文件夹删除(也可能不存在),拷贝features/org.sonatype.m2e.mavenarchiver.feature_0.17.2.201609252051到features ...
NULL 博文链接:https://wilian.iteye.com/blog/1992365
在Android中,当应用程序试图写入文件或数据到存储设备(如内部存储或SD卡)时,如果目标位置的空间不足以存放这些数据,就会抛出`android.system.ErrnoException`异常,并携带此错误码。 #### 二、常见触发场景 1...
在使用Mybatis框架时,你可能会遇到`org.apache.ibatis.exceptions.PersistenceException`这样的异常。这个异常通常表示在执行数据库查询操作时遇到了问题。本篇将详细分析这个问题并提供解决方法。 ### 问题概述 ...
Maven坐标:org.apache.httpcomponents:httpcore:4.4.13; 标签:apache、httpcomponents、httpcore、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。...
赠送jar包:netty-resolver-dns-4.1.65.Final.jar; 赠送原API文档:netty-resolver-dns-4.1.65.Final-javadoc.jar; 赠送源代码:netty-resolver-dns-4.1.65.Final-sources.jar; 赠送Maven依赖信息文件:netty-...
如在加密过程中出现下面异常: java.security.InvalidKeyException:illegal Key Size 请将两个jar文件按下列操作完成: 如果安装了JRE,将两个jar文件放到%JRE_HOME%\lib\security目录下覆盖原来的文件;如果安装了...
cmake-3.18.1 64位安装包。官网下载的。
org.apache.catalina.startup.ContextConfig.beforeStart Exception fixing docBase for context [/test] java.util.zip.ZipException: error in opening zip file at java.util.zip.ZipFile.open(Native Method) ...
Java连接达梦数据库驱动dm_jdbc: ...dm_jdbc\Dm7Dictionary.jar dm_jdbc\Dm7JdbcDriver14.jar dm_jdbc\Dm7JdbcDriver15.jar dm_jdbc\Dm7JdbcDriver16.jar dm_jdbc\Dm7JdbcDriver17.jar dm_jdbc\dmgeo_1.6.jar ...
CMake是一款跨平台的开源构建系统,用于管理软件构建过程。它的主要目标是提供一个统一的、用户友好的界面,让开发者能够在不同的操作系统和编译器之间无缝切换。CMake不直接进行编译,而是生成特定平台的构建系统,...
在XML Schema设计中,"cvc-complex-type.2.4.d: Invalid content was found" 是一个常见的错误信息,这通常意味着在解析XML文档时,遇到了不符合定义的复杂类型的内容。这个错误通常涉及到XML Schema的约束规则,即...
在使用MyBatisPlus进行开发时,可能会遇到一个常见的错误——`Invalid bound statement (not found)`。这个错误通常表示MyBatisPlus无法找到你尝试调用的Mapper接口的方法。本文将详细解析这个问题的原因以及提供...
Android异常详情介绍 这种异常我遇到以下两种情况: 1. java.lang.IllegalStateException: No wrapped connection. 2.java.lang.IllegalStateException: Adapter is detached. 原因: 1.单线程一次执行一个请求...
Maven坐标:org.apache.httpcomponents:httpclient:4.5.12; 标签:apache、httpcomponents、httpclient、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档...
2、可以转换,可以播放,但是有个异常:it.sauronsoftware.jave.EncoderException: video:0kB audio:1301kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.019516% 通过解读源码,在...
`*10 月 29 日 18:01:56.107:%PKI-3-CERTIFICATE_INVALID_EXPIRED:证书链验证失败。证书 (SN: 7E3446C40000000CBD95) 已过期。有效期结束于 10 月 14:38:08 UTC26 2021 对等证书验证失败 001A` 这表明 WLC...
InvalidArgument-The argument is invalid(解决方案).md