Java rcp开机自启动,java – 为Eclipse RCP应用程序添加Shutdown Hook...

注意:这个blog entry建议关闭钩子的以下实现:

The shutdown code must be run in the UI thread and should not be run if the workbench is being closed by other means. All dirty editors are automatically saved. This avoids prompting the user who is probably at home sleeping when their computer is shutdown. Finally the workbench is closed.

(所以不完全是你的场景,但实现仍然很有趣,因为它显示了如何在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

}

}

}

如你所说,它在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...

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值