我有一个独立的应用程序,我必须通过确认对话框提示用户保存他在尝试通过 start - > shutdown 。
I have a standalone application in which I have to prompt the user with an confirm dialog box to save the changes made by him when he tries to shutdown the system by start-->shutdown.
我通过使用 signalhandlers 来了解我们可以做到的。 可以帮助我如何使用信号处理程序
I came to know that by using signalhandlers we can do it. Can some one help me how to use signal handlers
推荐答案2012年5月更新(2年半之后)
Update May 2012 (2 and half years later)
Trejkaz 评论:
在当前版本的Java上,此信号处理代码失败,因为 INT 信号是由VM保留或操作系统。 此外,当某些内容请求应用程序关闭时,其他任何有效的信号名称都不会触发(我只是仔细测试了所有我能找到的内容......) b $ b关闭钩子大部分都有效但我们发现在我们的情况下它不会触发,所以下一步显然是在JVM的后面注册一个处理程序
On current versions of Java this signal handling code fails because the "INT" signal is "reserved by the VM or the OS". Additionally, none of the other valid signal names actually fire when something requests the application to close (I just painstakingly tested all of the ones I could find out about...) The shutdown hook mostly works but we find that in our case it isn't firing, so the next step is obviously to resort to registering a handler behind the JVM's back
章节集成信号和异常处理的 HotSpot VM故障排除指南提到信号 SIGTERM , SIGINT , SIGHUP 仅适用于Solaris OS和Linux。 仅提到Windows上的异常处理。
The chapter "Integrating Signal and Exception Handling" of the "Troubleshooting Guide for HotSpot VM" mentions the signals "SIGTERM, SIGINT, SIGHUP" only for Solaris OS and Linux. Only Exception Handling on Windows are mentioned.
原始答案(2009年9月)
Original answer (Sept 2009)
a ShutdownHook 应该能够处理这种情况
a ShutdownHook should be able to handle that case
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { // what you want to do } }));(告诫) 参见:
(with caveats) See also:
- Java信号处理和终止(链接已死,请参阅存档或镜像)
- java退出信号处理:
- Java signal handling and termination (link is dead, see archive or mirror)
- java exit signal handling:
作为简单信号处理的例证:
as an illustration of simple signal handling:
public class Aaarggh { public static void main(String[] args) throws Exception { Signal.handle(new Signal("INT"), new SignalHandler () { public void handle(Signal sig) { System.out.println( "Aaarggh, a user is trying to interrupt me!!"); System.out.println( "(throw garlic at user, say `shoo, go away')"); } }); for(int i=0; i<100; i++) { Thread.sleep(1000); System.out.print('.'); } } }更多推荐
使用“TERM”的信号处理
发布评论