检测Java应用程序何时关闭 [英] Detecting when a Java application closes

查看:125
本文介绍了检测Java应用程序何时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<$ p $

p> //应用程序关闭时拦截
private void Form1_FormClosing(object sender,FormClosingEventArgs e)
{
//从MIDI使用回收资源
if (MIDIControl.CleanUp())
{
Logger.Add(成功关闭表单关闭\\\
);
}
else
{
Logger.Add(无法关闭Form Close上的所有资源);
}
}

我试图执行相同的技术Java版本似乎不起作用,我已经尝试调试,并且不会在方法名称上的断点上挂起:

  //当应用程序关闭时拦截
public void windowClosing(WindowEvent we)
{
//从MIDI使用回收资源
if(_midiInstance.CleanUp() )
{
Logger.Add(ShutDown成功关闭资源);
}
else
{
Logger.Add(无法关闭ShutDown上的所有资源);
}
System.exit(0);
}

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



感谢您的时间。

解决方案

您是否通过 addWindowListener() windowClosing()方法的类c $ c>?



除了使用窗口确定应用程序状态不是一个好的风格。 Java明确允许你挂起关机过程:

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

@Override
public void run(){
//将你的代码放在
}

});


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.

解决方案

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
    }

});

这篇关于检测Java应用程序何时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆