Ending an NSTask

Normally, you want the task that you’ve launched to run to completion. When the task exits, the corresponding NSTask object posts an NSTaskDidTerminateNotification to the default notification center. You can add one of the custom objects in your program as an observer of the notification and check the task’s exit status (using terminationStatus) in the observer method. For example:

-(id)init {
    self = [super init];
    [[NSNotificationCenter defaultCenter] addObserver:self
    return self;
- (void)checkATaskStatus:(NSNotification *)aNotification {
    int status = [[aNotification object] terminationStatus];
    if (status == ATASK_SUCCESS_VALUE)
        NSLog(@"Task succeeded.");
        NSLog(@"Task failed.");

If you need to force a task to end execution, send a terminate message to the NSTask object. If the NSTask object gets released, however, NSTaskDidTerminateNotification does not get sent, as the port the message would have been sent on was released as part of the task release.