I’ve usually got all kinds of shenanigans (like timers) going on when the user quits the app. To help, I have a flag in the App object called App.IsQuiting that I set during App.CancelClose. Anything complex checks this and aborts.
However, sometimes during quit when I check the flag, I find that the app object is nil, likely because it’s being destroyed as the app shuts down. This of course leads to worse shenanigans. It’s like having the floor fall out of the house as you’re trying to run out the door. Damn my slow feet!
What about setting a status flag to “QuitPending”, set a timer for a closeAllGracefully() routine and return True (cancel the quit). After you have take care of any necessary clean up routines in closeAllGracefully(), you set the flag to “ReadyToQuit” and Quit(). Now you can return False in the App.CancelClose() and the system will destroy everything. Something like that.
No OS involved. You just aborted the request to quit, and postponed it to your clean up routine to take care of. All your threads, timers, etc still running and you could take care of them there properly. You should return False after (App.CancelClose()), in the next quit(), launched by your clean up routine and then everything will be destroyed properly.
I’d actually like to get the official word on this. Like Stephen, I’d have thought canceling the quitting process wouldn’t be a great idea. Pretty sure Matt Neuburg’s old book described the quitting process clearly, but it’s possibly inaccurate now.
Most of the time. It is possible for the OS to initiate the quit sequence. Then it might conclude that your app is hung if it takes too much time to clean itself up. But for user-initiated quit from within the app, you are correct, the OS doesn’t get involved.
But that shouldn’t hurt anything (at least on the Mac). Apps that have unsaved documents, for instance, often will stop a shutdown sequence. The users gets an error message that “App x prevented the shutdown” or something to that effect. Then it’s up to the user to shut that particular app down in a safe manner.
Of course, newer apps are designed to auto-save all unsaved documents and reopen them on next launch, so that kind of shutdown abort happens less and less these days.