public void widgetSelected(SelectionEvent selectionevent) {
if (selectionevent.widget.equals(button)) {
showProgressBar();
setBarValue(60);
final String eValue = e.getText();
final String fValue = f.getText();
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
try {
byte[] resultData = null;
byte[] authorizeData = factory.CreateAuthorizeData(eValue, fValue);
ConnectionServiceThread.sendToAuthServer(authorizeData);
display.asyncExec(new Runnable() {public void run() {setBarValue(50);}});//设置进度条进度
resultData = ConnectionServiceThread.recevieFromAuthServer();
final SessionBean result = factory.HandleSessionData(resultData);
if(result.getStates() == Constants.LOGIN_SUCESSFUL){
//登陆成功
display.asyncExec(new Runnable() {public void run() {display.setData(Constants.SESSION_ID_KEY, result.getSessionId());}});
new MainIndex();
}else{
//登陆失败
display.asyncExec(new Runnable() {public void run() {label.setText("用户名或密码错误,请重试");}});
}
display.asyncExec(new Runnable() {public void run() {setBarValue(100);}});
} catch (IOException e1) {
// 处理网络异常
display.asyncExec(new Runnable() {public void run() {label.setText("数据访问错误");}});
}catch(SecurityException e2){
display.asyncExec(new Runnable() {public void run() {label.setText("未被允许访问网络");}});
} finally {
display.asyncExec(new Runnable() {public void run() {closeProgressBar();}});//关进度条
}
}
}).start();
}
}
最近在弄SWT,界面是其实是单线程的。不能有任何阻塞,事务在异步的时候也只能新建线程来处理,但是此线程有不能修改任何界面相关的数据。于是出现了上面大量的重复代码。不知道这样处理对不?