2

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.

  • Incidentally, is there a reason you're doing this on a thread? Tasks (processes) already tend to get scheduled on other processors, and run loops naturally handle infrequent discrete events like NSTasks finishing. – Peter Hosey Apr 05 '10 at 23:46
  • After searching a long time I found that not the NSTask is being unresponsive but something else. This allows me to get rid of the thread. Thank you for asking why I needed one, otherwise I wouldn't have found the problem. – Michael Matheus Apr 06 '10 at 08:10

1 Answers1

1

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
  • 95,783
  • 15
  • 211
  • 370
  • 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 Apr 05 '10 at 16:14
  • The object in the second thread (the one I specified in detachNewThreadSelector:toTarget:withObject:) – Michael Matheus Apr 05 '10 at 18:50
  • 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 Apr 05 '10 at 23:38