检测 Java 应用程序何时关闭

发布于 2024-11-02 05:56:04 字数 1138 浏览 5 评论 0原文

我试图检测 Java 应用程序何时即将关闭,以便我可以执行释放资源的方法,我在 C# 中完成了如下操作:

//Intercept when the application closes
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Reclaim resources from MIDI usage
            if (MIDIControl.CleanUp())
            {
                Logger.Add("Closed resources successfully on Form Close \n");
            }
            else
            {
                Logger.Add("Failed to close all resources on Form Close.");
            }
        }

我尝试在 Java 版本中执行相同的技术,但似乎没有为了工作,我尝试过调试,并且它不会在方法名称上放置的断点处挂起:

//Intercept when the application closes
        public void windowClosing(WindowEvent we)
        {
            //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);
        }

我是否在等待错误的事件?我将如何以与 C# 版本相同的方式执行适当的方法。

感谢您抽出时间。

I am trying to detect when the Java application is about to close so I can perform methods to release resources, I have done it in C# as follows:

//Intercept when the application closes
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Reclaim resources from MIDI usage
            if (MIDIControl.CleanUp())
            {
                Logger.Add("Closed resources successfully on Form Close \n");
            }
            else
            {
                Logger.Add("Failed to close all resources on Form Close.");
            }
        }

I've tried to perform the same technique in the Java version yet it doesn't seem to work, I've tried debugging and it doesn't suspend on the breakpoint placed on the method name:

//Intercept when the application closes
        public void windowClosing(WindowEvent we)
        {
            //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);
        }

Am I waiting for the wrong event? How would I go about performing the appropiate method in the same way as the C# version.

Thanks for your time.

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

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

发布评论

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

评论(1

寂寞美少年 2024-11-09 05:56:04

您是否通过 addWindowListener() 注册了包含 windowClosing() 方法的类?

除此之外,使用窗口来确定应用程序状态并不是一个好的风格。 Java 明确允许您挂钩关闭进程:

Runtime.getRuntime().addShutdownHook(new Thread() {

    @Override
    public void run() {
        // place your code here
    }

});

Have you registered the class containing windowClosing() method as via addWindowListener()?

Besides that using a window for determining the application state is not a good style. Java explicitly allows you to hook the shutdown process:

Runtime.getRuntime().addShutdownHook(new Thread() {

    @Override
    public void run() {
        // place your code here
    }

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