applicationWillTerminate 和扩展坞但想要取消此操作

发布于 2024-10-09 01:30:25 字数 324 浏览 10 评论 0原文

我的应用程序中有一些单独的线程需要正确拆除。 如果用户在文件菜单中选择退出菜单项,我会抛出一个错误,提示...请在退出之前停止您正在执行的其他操作。但是,如果用户从 Mac 上的应用程序扩展坞退出该应用程序,该应用程序就会退出并最终崩溃,因为其他线程仍在尝试执行其操作而没有被正确拆除。

我尝试了 -applicationShouldTerminate: 但在使用应用程序停靠退出方法的情况下它不会触发...如果文档脏了。如果文档被清理或保存,那么我的对话框会正确弹出。

所以我想真正的问题是:在我们发现文档仍然很忙之前,如何停止“您想保存文档”查询吗?

谢谢

I have some separate threads going in my application that need to be torn down correctly.
IF the user selects the quit menu item in file menu I throw up an error that says... please stop the other things your are doing before quitting. However if the user quits the app from the app dock on the mac the app just quits and ends up crashing because the other threads are still trying to do their thing without being torn down correctly.

I tried -applicationShouldTerminate: but it doesnt trigger in the case with the app dock quit method... if the document is dirty. IF the document is cleaned aka saved then my dialog pops up correctly.

SO I guess the real question is: how do I stop the 'do you want to save your document' query before we find that the document is still busy?

thanks

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

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

发布评论

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

评论(2

岁月染过的梦 2024-10-16 01:30:25

在 NSApplication 委托中使用 -applicationShouldTerminate: 是正确的做法。

但是,对于在 Mac OS X 10.6 及更高版本上构建的应用程序,您可能会遇到一个名为“突然终止”的功能。 Foundation Release Notes 中对此进行了非常简单的描述

结果是您的后台任务应该防止突然终止:

- (void)longRunningTask {
    NSProcessInfo *processInfo = [NSProcessInfo processInfo];
    [processInfo disableSuddenTermination];

    // real task work

    [processInfo enableSuddenTermination];
}

这样,当您要求应用程序在繁忙时退出时,将适当地发送 -terminate: ,这将反过来调用您的 NSApplication delegate 的 < code>-applicationShouldTerminate:,这将允许您优雅地停止任务或要求用户取消它们。

如果您在 NSOperation 或其子类中执行长时间运行的任务,最好只是一般性地添加此支持,而不是将其单独添加到所有任务中。 (无论任务如何停止、完成还是取消,都不要忘记重新启用突然终止。)

Using -applicationShouldTerminate: in your NSApplication delegate is the right thing to do.

However, for applications built on Mac OS X 10.6 and later, there's a feature called Sudden Termination that you're probably running into. It's described pretty straightforwardly in the Foundation Release Notes.

The upshot is that your background tasks should prevent sudden termination:

- (void)longRunningTask {
    NSProcessInfo *processInfo = [NSProcessInfo processInfo];
    [processInfo disableSuddenTermination];

    // real task work

    [processInfo enableSuddenTermination];
}

This way, your application will be sent -terminate: appropriately when you ask it to quit while it's busy, which will in turn invoke your NSApplication delegate's -applicationShouldTerminate:, which will allow you to stop your tasks gracefully or ask the user to cancel them.

If you're performing your long-running tasks in an NSOperation or subclass thereof, it would be good to just add this support generically rather than add it to all your tasks individually. (Don't forget to re-enable sudden termination no matter how your task stops, whether it finishes or is canceled.)

夜清冷一曲。 2024-10-16 01:30:25

之前的 StackOverflow 帖子可能会有所帮助:

应用程序退出事件

当退出菜单项时,您可以覆盖默认事件处理程序被选中。

This previous StackOverflow post may help:

Application exit event

You can override the default event handler when the Quit menu item is chosen.

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