On Feb 22, 2009, at 11:35 AM, Charles E. Heizer wrote:

I reworked my test app so that I could isolate the threads and to see what's going on a little better, and I'm now using a NSTimer and a NSRunLoop. The issue I'm seeing is that for every time I call NSThread so not to block the run loop it runs it's course but when it completes it leaves a hanging thread. So every 60 seconds I see an additional thread get added and never drops it once completed.

You don't need to use a secondary thread to avoid blocking the main thread when you use an NSTask. Just don't use the blocking methods of NSTask or NSFileHandle; use the asynchronous versions.

That is, don't invoke -[NSFileHandle readDataToEndOfFile]. Register for the NSFileHandleReadToEndOfFileCompletionNotification notification and invoke -readToEndOfFileInBackgroundAndNotify. Similarly, don't invoke -[NSTask waitUntilExit]. Register for the NSTaskDidTerminateNotification notification.

If you decide to continue using a secondary thread, try invoking - [NSTask waitUnitExit] after you've read the task's output. I believe that NSTask needs the run loop of the thread from which it is launched to run in order for it to detect the termination of the task, deliver its notification, and perform some housekeeping cleanup. The fact that you're not running the run loop of the thread is the reason it's failing to clean up. The -waitUntilExit method runs the run loop, so it should be sufficient.

Regards,
Ken

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to