英文:
Handling application force quit in Swift for macOS applications
问题
"我目前正在使用Swift开发macOS应用程序,并且在应用程序终止时需要执行某个特定操作(在我的情况下,重新映射一个键)。我可以处理应用程序正常退出的情况,使用AppDelegate
中的applicationWillTerminate(_:)
方法,但当应用程序被强制退出,例如通过活动监视器时,这个方法不会被调用。
我了解当应用程序被强制退出时,操作系统会发送一个SIGKILL
信号,应用程序无法忽略或处理,因此它会立即停止,不会执行任何进一步的代码。但是,是否有任何解决方法或替代方法,可以让我处理这种情况或实现类似的效果?我会感激任何建议或见解。"
英文:
"I'm currently developing a macOS application using Swift and I need to perform a certain action (in my case, remapping a key) when the application is terminated. I am able to handle the situation where the application is quit normally using the applicationWillTerminate(_:)
method in the AppDelegate
, but this doesn't get called when the application is force quit, such as through the Activity Monitor.
I understand that when an application is forcibly quit, the operating system sends it a SIGKILL
signal that it cannot ignore or handle, so it immediately stops without executing any further code. However, is there any workaround or alternative approach that would allow me to handle this scenario or achieve a similar effect? I would appreciate any suggestions or insights."
答案1
得分: 3
你可以运行一个单独的进程来监视第一个进程,并在其停止运行时进行检测。在正常关闭期间,主程序可以终止监视进程。这也可以是循环的,因此主程序监视监视进程,并在监视进程被终止时重新启动它。或者,您可以使用启动守护程序在需要时自动重新启动监视进程。
英文:
You can run a separate process that monitors the first, and detects when it stops running. During normal shutdown, the main program can terminate the monitor. This can also be circular, so that the main program watches the monitor and relaunches it if the monitor is killed. Or you can use a Launch Daemon to automatically re-launch the monitor if needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论