我有问题。在我的(基于eclipse 4的)GUI上,我有两个对象:
Button button = new Button(groupInitialize, SWT.NONE);
ProgressBar bar= new ProgressBar(group, SWT.SMOOTH);
我为按钮设置了一个列表器,以便在按下按钮的任何时候开始详细说明。在此详细说明中,状态栏必须更新。
// Button listener definition!
button .addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(final Event event) {
Runnable run = new Runnable() {
@Override
public void run() {
Display display = PlatformUI.getWorkbench().getDisplay();
display.asyncExec(new Runnable() {
public void run() {
myLongLastingMethod();
}
});
}
};
new Thread(run).start();
}
});
这就是我在myLongLastingMethod()中所做的:
private void myLongLastingMethod() {
action1();
update();
action2();
update();
action3();
update();
}
最后是更新方法(应该更新进度条):
已更新
private void update() {
if (progressBar.isDisposed()) {
return;
}
int selection = progressBar.getSelection();
progressBar.setSelection(++selection);
}
我确定我做错了什么......有什么想法/帮助它为什么不起作用?
答案 0 :(得分:1)
您的后台主题Runnable
正在使用display.asyncExec
来调用您的长时间运行方法。 display.asyncExec
运行用户界面线程中的代码,因此在该代码运行时,UI中不会发生任何其他事情。您应该只使用asyncExec
来运行更新UI的短代码。
只需在后台线程中直接拨打myLongLastingMethod
,而无需asyncExec
来电。
所以:
public void handleEvent(final Event event) {
Runnable run = new Runnable() {
@Override
public void run() {
myLongLastingMethod();
}
};
new Thread(run).start();
}