views:

211

answers:

1

I'm trying to get the notification NSTaskDidTerminateNotification in my multithreaded app but I can't get it working. It does seem to work when I tested it on a single threaded app. In init I have [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(taskDidEnd:) name: NSTaskDidTerminateNotification object: myTask]; and I'm quite sure that it gets called because other objects (like myTask) are being initiated there. And the taskDidEnd: method is defined as

- (void)taskDidEnd: (NSNotification *)aNotification
{
     NSLog(@"Task succeeded.");
}

And in dealloc the observer gets removed.

This all happens in an object which is initiated inside a separate thread and I would like to receive that notification inside the same object.

+1  A: 

Did you run the run loop on that thread? If not, NSTask won't notice that the task ended (or the task won't have ended yet) and won't post the notification.

Peter Hosey
I have tried [[NSRunLoop currentRunLoop] run] from within init but that doesn't seem to help. Am I using it in the wrong way?
Michael Matheus
What are you initing?
Peter Hosey
The object in the second thread (the one I specified in detachNewThreadSelector:toTarget:withObject:)
Michael Matheus
You're initing the object *after* you send it a message? That makes no sense. You need to run both the task and the run loop on that thread if you want to receive the notification on that thread.
Peter Hosey