Computing and SWT
As Swing
I create SwingWorkers
and use invokeLater
to perform calculations in time, without disturbing Swings GUI thread. How is this done in SWT? I'm writing code using Callable and Future, but I don't think this will cut it down:
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();
}
}
}
});
}
}
EDIT:
I am trying to add the following snippet to my intensive computation class:
final Display display = this.shell.getDisplay();
display.asyncExec(new Runnable() {
public void run() {
if (!display.isDisposed()) {
display.readAndDispatch();
}
}
});
I will update when I have more details. Man I missed Swing ...
source to share
First, you should avoid subjective impressions in your question, they do not help to answer the question, second for a snippet, how to access widgets from another thread, see here why it is necessary, see here and if you are using SWT when combined with the Eclipse RCP Framework, you should consider handling long processes in Eclipse Jobs .
source to share
You want to call asyncExec
on the display. Please read the SWT Threading Issues doc for more details.
Here is the relevant code snippet from that document that shows how to use it to redraw a window:
// 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
...
source to share