Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

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
            selector:@selector(checkATaskStatus:)
            name:NSTaskDidTerminateNotification
            object:nil];
    return self;
}
 
- (void)checkATaskStatus:(NSNotification *)aNotification {
    int status = [[aNotification object] terminationStatus];
    if (status == ATASK_SUCCESS_VALUE)
        NSLog(@"Task succeeded.");
    else
        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.



< Previous PageNext Page > Hide TOC


Last updated: 2006-04-04




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice