本文介绍了为Eclipse RCP应用程序添加关闭挂钩的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个使用与内存数据库连接的RCP应用程序。有一种情况,当关闭窗口时,应用程序被杀死,而不给它一个关闭数据库的连接的机会。 我研究了一点,似乎添加关闭挂钩是检测此事件并在Java应用程序中进行清理的最佳方法。但是,如果您有RCP应用程序,可能有多个编辑器打开,那么正确的处理方法是什么?解决方案注意:此博客条目建议关闭挂钩的以下实现: (所以不是完全你的场景,但实现仍然很有意思它显示如何在UI线程中运行它) 私有类ShutdownHook扩展Thread { @Override public void run(){ try { final IWorkbench workbench = PlatformUI.getWorkbench(); final显示= 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关闭代码必须访问 //工作台使用UI线程} else { IWorkbenchPage [] page = workbenchWindow .getPages(); for(int j = 0; j< pages.length; j ++){ IEditorPart [] dirtyEditors = pages [j] .getDirtyEditors(); for(int k = 0; k dirtyEditors [k] .doSave(new NullProgressMonitor()); } } } } } }); display.syncExec(new Runnable(){ public void run(){ workbench.close(); } }); } } catch(IllegalStateException e){ //忽略} } } / pre> 正如你所说,在IApplication中设置: public class IPEApplication implements IApplication { public Object start(IApplicationContext context)throws Exception { final显示display = PlatformUI.createDisplay(); Runtime.getRuntime()。addShutdownHook(new ShutdownHook()); } //启动工作台... } } I have an RCP application that uses a connection to a in-memory database. There is one circumstance that, when shutting down windows, the application is killed without giving it a chance to close the connection to the database.I researched a little and it seems that adding a Shutdown hook is the best way to detect this event and do cleanup in a Java application. However, what is the correct way to do process this if you have an RCP application, possibly with multiple editors open? 解决方案 Note: this blog entry suggests the following implementation for the shutdown hook:(so not exactly your scenario, but the implementation is still interesting in that it shows how to run it within the UI thread)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 } }}It is set, as you said, 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... }} 这篇关于为Eclipse RCP应用程序添加关闭挂钩的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 11:08