在Swing
中,我创建SwingWorkers
或使用invokeLater
进行时间密集型计算,而不会影响Swings GUI线程。如何在SWT中做到这一点?我正在使用Callable和Future编写代码,但我不认为这会削减它:
class MyClass extends ViewPart {
.
.
.
@Override
public void createPartControl(final Composite arg0) {
this.runScenarioItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
final ScenarioDialog scenarioDialog = new ScenarioDialog(arg0.getShell(), SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL);
final Scenario scenario = scenarioDialog.open();
if (suvConnection.isConnected()) {
runScenarioItem.setEnabled(false);
try {
final ScenarioRunner runner = new ScenarioRunner(suvConnection, scenario);
final ExecutorService executor = new ScheduledThreadPoolExecutor(1);
final Future<Integer> future = executor.submit(runner);
System.out.println("result of callable = " + future.get());
runScenarioItem.setEnabled(true);
}
catch (final Exception e1) {
e1.printStackTrace();
}
}
}
});
}
}
编辑:
我正在尝试将以下代码段添加到我的密集计算类中:
final Display display = this.shell.getDisplay();
display.asyncExec(new Runnable() {
public void run() {
if (!display.isDisposed()) {
display.readAndDispatch();
}
}
});
当我有更多信息时,我会更新。男人,我想念Swing ......
答案 0 :(得分:3)
您想在人机界面上拨打asyncExec
。有关详细信息,请阅读SWT Threading Issues文档。
以下是该文档的相关示例代码段,其中显示了如何使用它重绘窗口:
// do time-intensive computations
...
// now update the UI. We don't depend on the result,
// so use async.
display.asyncExec (new Runnable () {
public void run () {
if (!myWindow.isDisposed())
myWindow.redraw ();
}
});
// now do more computations
...
答案 1 :(得分:3)