Nota: questo blog entry suggerisce il seguente implementazione per il gancio di arresto:
Il codice di arresto deve essere eseguito nel thread UI e non dovrebbe essere eseguito se la pialla è chiusa con altri mezzi. Tutti gli editor dirty vengono automaticamente salvati. Questo evita di chiedere all'utente che è probabilmente a casa a dormire quando il computer è spento. Finalmente il banco di lavoro è chiuso.
(quindi non esattamente lo scenario, ma l'implementazione è ancora interessante in quanto mostra come eseguire all'interno del thread UI)
private class ShutdownHook extends Thread {
@Override
public void run() {
try {
final IWorkbench workbench = PlatformUI.getWorkbench();
final Display display = PlatformUI.getWorkbench()
.getDisplay();
if (workbench != null && !workbench.isClosing()) {
display.syncExec(new Runnable() {
public void run() {
IWorkbenchWindow [] workbenchWindows =
workbench.getWorkbenchWindows();
for(int i = 0;i < workbenchWindows.length;i++) {
IWorkbenchWindow workbenchWindow =
workbenchWindows[i];
if (workbenchWindow == null) {
// SIGTERM shutdown code must access
// workbench using UI thread!!
} else {
IWorkbenchPage[] pages = workbenchWindow
.getPages();
for (int j = 0; j < pages.length; j++) {
IEditorPart[] dirtyEditors = pages[j]
.getDirtyEditors();
for (int k = 0; k < dirtyEditors.length; k++) {
dirtyEditors[k]
.doSave(new NullProgressMonitor());
}
}
}
}
}
});
display.syncExec(new Runnable() {
public void run() {
workbench.close();
}
});
}
} catch (IllegalStateException e) {
// ignore
}
}
}
Si trova, come hai detto, in the IApplication:
public class IPEApplication implements IApplication {
public Object start(IApplicationContext context) throws Exception {
final Display display = PlatformUI.createDisplay();
Runtime.getRuntime().addShutdownHook(new ShutdownHook()); }
// start workbench...
}
}
Ho anche quello, ma se la JVM termina questa sezione non viene chiamato. Ad esempio, come risultato di System.exit() –