如何使用Apple的handleQuit方法? [英] How to use Apple's handleQuit method?

查看:144
本文介绍了如何使用Apple的handleQuit方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个Java应用程序,将数据保存到.data文件.我有一个窗口监听器,它监听应用程序关闭以触发代码以将数据保存到文件中.当我按下专用的退出按钮或在窗口上按下红色的"X"时,一切都很好.但是,当用户选择命令+ q路由时,情况就会变糟.退出应用程序,但不保存数据.如何正确实现apple的handleQuit(Application Event e)方法来解决此问题?

I made a java application that saves data to a .data file. I have a Window Listener that listens for the application to close in order to fire the code to save the data to the file. When pressing the dedicated quit button I have, or pressing the red "X" on the window, everything is fine. However, when the user opts for the command + q route things go sour. The application is quit, but the data is not saved. How do I correctly implement apple's handleQuit(Application Event e) method to fix this?

推荐答案

在这种情况下,您想要的是关闭挂钩.关闭挂钩监听OS信号以关闭应用程序,并在发送该信号时触发.关闭钩子几乎可以运行任何代码.

What you want in that case is a shutdown hook. A shutdown hook listens for the OS signal to close the app and is triggered when this signal is sent. A shutdown hook can run pretty much any code.

您可以连接内置的红色"X"按钮以关闭应用程序(而不是保存文件),关闭挂钩将捕获请求并负责保存文件.

You can wire your built-in red "X" button to close the app (instead of saving the file), and the shutdown hook will catch the request and take care of saving the file.

唯一的警告是,关闭挂钩应该由执行时间不会很长的代码组成.因此,保存文件的时间不应超过一两秒钟,也不应使用用户必须在关机钩子中确认的确认对话框,因为在用户识别之前,它可能花费不确定的时间对话框.

The only caveat is that shutdown hooks are supposed to be made up of code that doesn't take very long to execute. So, the save of your file shouldn't take more than a second or two, and you shouldn't use confirmation dialog boxes that the user must acknowledge in a shutdown hook, because it can take an indefinite amount of time before the user recognizes the dialog.

关闭钩子应该短暂存在的原因是,当请求关闭应用程序时,操作系统通常希望它在合理的时间内关闭.如果没有,例如在Windows中,则操作系统可能会显示应用程序无响应..."消息之一.

The reason why shutdown hooks should be short lived is that when an application is requested to shutdown, the OS generally expects it to shutdown in a reasonable amount of time. If it doesn't, for example in Windows, the OS might display one of those, "Application isn't responding..." messages.

最后,您稍后可能会遇到此问题,您可能想知道如何从任务管理器(或OS X上的强制退出应用程序"对话框)捕获强制退出"请求.好吧,你不能抓住那些,你不应该尝试!虽然可以禁用诸如在强制退出"菜单中列出您的应用之类的功能,是一个完整的hack,应该不惜一切代价避免.如果您在设计应用程序时尝试避开应该始终对用户和管理员可用的选项,那么就很可能表明您的应用程序设计不佳和/或行为不当.想象一下,如果您安装的应用程序具有这种行为-您是否会认为程序员在试图赋予其应用程序无与伦比的品质时是懒惰的或可能是恶意的?

Finally, and you might run into this question later, you might wonder how to catch a "Force Quit" request from the Task Manager (or "Force Quit Applications" dialog on OS X). Well, you can't catch those, and you shouldn't try! While it is possible to disable things like listing your app in the Force Quit menu, this is a complete hack and should be avoided at all costs. If you're designing your app in a way that tries to circumvent options that should always be available to users and admins then that's a strong indication that your app is poorly designed, and/or a bad actor. Imagine if you installed an app that behaved in this way - wouldn't you think the programmer was being lazy or possibly malicious in trying to give their app un-killable qualities?

此外,强制退出是强制性的(不优雅的)关闭,仅应在挂起且无法正常退出的应用程序上使用.操作系统需要具有强制退出的选项,以便用户或管理员可以杀死失控或无响应的应用程序.如果您的用户强行退出了您的应用程序,他们可能会误认为不希望强行退出,或者您的应用程序设计中有某些因素使强行退出比正常退出您的应用程序更有利.如果是这种情况(例如,您从用户那里得知他们出于某种原因而强制退出),通常表明您的应用程序部分设计不佳,无法满足用户的期望.

Also, force quitting is a forcible (ungraceful) shutdown that should only be used on applications when they are hung and won't quit normally. OSs need to have a force quit kind of option so that a user or admin has a way to kill a runaway or unresponsive app. If your users are force quitting your app they either misunderstand that force quitting isn't desirable, or there's something about the design of your app that makes force quitting more favorable than quitting your app normally. If this is the case (e.g. you hear from users that they force quit for one reason or another), it's usually an indication that portion of your app is poorly designed to match user's expectations.

这篇关于如何使用Apple的handleQuit方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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