Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

davbeck

macrumors regular
Original poster
May 16, 2008
104
0
Reno, NV
What is the best way to restart my Cocoa app? I saw one example online but it uses kill which will force quite the app.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
You could write a daemon process to handle this if you don't want to use kill. The daemon could launch with args for the process name/pid to quit. You could then use Apple Events (e.g. "tell application 'App' to quit") to handle this smoothly at first and wait for a termination notification (e.g. NSWorkspaceDidTerminateApplicationNotification). If that's not received within a given amount of time you could then kill it like normal. 10.6 adds a NSRunningApplication class which would make this entire process easier.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
In your app, create a pipe. Start a child process. Have it read the pipe from its parent, your app. When the pipe gets an EOF, it should relaunch your app.

In your app, after the child process is launched, do your app's normal cleanup and termination, but leave the sending end of the pipe open.

When your app exits, the system will automatically close the sending end of the pipe, which your lingering child then detects as an EOF on its reading end, which triggers its action of relaunching your app.

That's the gist of it. You may have to arrange for some signals to be ignored in the child. You may also want a timeout in the child, to abort if an EOF doesn't appear within X seconds (BSD alarm() function, SIGALRM signal). Experiment.


The source for the Sparkle framework has code for a shell script that does approximately what I outlined above, although it polls rather than reading a pipe. Or at least it used to. It may be different now.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.