您的位置:首页 > 其它

关于SWT开发的一个坑——Invalid thread access

2015-08-16 00:00 211 查看
摘要: 解决方案:Display.getDefault().syncExec(new Runnable() {
public void run() {
allTxt.setText("登录成功....");
}
}); ​

Button createCodeBtn = new Button(shell, SWT.CENTER);
createCodeBtn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {

try {
if(txtXMLSrc.getText().trim().equals("") || txtCodeTo.getText().trim().equals("")) {
lblErrorMsg.setVisible(true);
new Timer().schedule(new TimerTask() {
public void run() {
//								Display.getDefault().syncExec(new Runnable() {
//								    public void run() {
lblErrorMsg.setVisible(false);
//								    }
//							    });
}
}, 3 * 1000);
return;
}
Main.geneCode(txtXMLSrc.getText() ,txtCodeTo.getText());
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
});




这里我在按钮createCodeBtn点击事件中写了个定时器new Timer(),定时器是用的匿名类实现的schedule方法,定时器作用是过3s就把lblErrorMsg设置为隐藏。一开始报这个Invalid thread access我还以为是因为匿名内部类传参有问题,于是去查,试过各种final方法后来还是不行,后来我偶然一查发现有篇帖子介绍SWT这个坑,于是试了下,的确就是这个Display.getDefault().syncExec(new Runnable()的坑。

上面代码我注释掉这几行就会报错,正确做法就是不注释。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SWT Invalid thread access