1

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

theprojectabot
  • 1,163
  • 12
  • 19

2 Answers2

8

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.)

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
0

This previous StackOverflow post may help:

Application exit event

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

Community
  • 1
  • 1
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144