检测 Java 应用程序何时关闭
我试图检测 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否通过
addWindowListener()
注册了包含windowClosing()
方法的类?除此之外,使用窗口来确定应用程序状态并不是一个好的风格。 Java 明确允许您挂钩关闭进程:
Have you registered the class containing
windowClosing()
method as viaaddWindowListener()
?Besides that using a window for determining the application state is not a good style. Java explicitly allows you to hook the shutdown process: