调用 System.exit(0) 后应用程序继续运行 - Java

发布于 11-02 17:06 字数 1048 浏览 4 评论 0 原文

我正在尝试在应用程序关闭之前清理应用程序中的资源,继我之前的问题之后(检测 Java 应用程序何时关闭)我已经实现了以下代码,可以完美地执行清理操作。

//Intercept when the application closes
            Runtime.getRuntime().addShutdownHook(new Thread()
            {
                @Override
                public void run()
                {
                    //Reclaim resources from MIDI usage
                    if(_midiInstance.CleanUp())
                    {
                        Logger.Add("Closed resources successfully on ShutDown");
                    }
                    else
                    {
                        Logger.Add("Failed to close all resources on ShutDown");
                    }
                    System.exit(0);
                }
            });

虽然 System.exit(0);调用被理解并处理后,应用程序继续运行,只是没有可见的 GUI。我考虑过将 System.exit(0) 调用放在线程之外,但它超出了范围,没有任何其他线程或流在运行。

在连接 ShutDown 事件以确保所有内容都关闭时,我是否需要采取额外的步骤?

感谢您抽出宝贵的时间,我非常感激。

I'm trying to clean up resources in my application before it shuts down, following on from my previous question (Detecting When A Java Application Closes) I have implemented the following code which performs the cleanup operation perfectly.

//Intercept when the application closes
            Runtime.getRuntime().addShutdownHook(new Thread()
            {
                @Override
                public void run()
                {
                    //Reclaim resources from MIDI usage
                    if(_midiInstance.CleanUp())
                    {
                        Logger.Add("Closed resources successfully on ShutDown");
                    }
                    else
                    {
                        Logger.Add("Failed to close all resources on ShutDown");
                    }
                    System.exit(0);
                }
            });

Although the System.exit(0); call is understood and processed the application continues to run, just without a visiable GUI. I've thought about placing the System.exit(0) call just outside of the Thread but then it's out of scope, there aren't any other threads or streams running.

Is there an additional step I need to take when hooking in to the ShutDown event to ensure everything closes?

Thanks for your time, I greatly appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花伊自在美 2024-11-09 17:06:53

阅读您的其他问题后,您似乎可能没有调用 "="">dispose() 在您的窗口上。如果属实,那就可以解释问题的原因。

After reading your other question, it seems like your are probably not calling dispose() on your window(s). If true, that would explain the cause of your problem.

当梦初醒 2024-11-09 17:06:53

您需要覆盖窗口关闭按钮:

            //overriding the windowClosing() method will allow the user to click the close button
    addWindowListener(
            new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    });

通过这样做,程序将关闭而不仅仅是变得不可见。

You need to over ride the windows close button:

            //overriding the windowClosing() method will allow the user to click the close button
    addWindowListener(
            new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    });

By doing this the program will close not just become invisible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文