Re: Question on threads running in my Foundation tool
Re: Question on threads running in my Foundation tool
- Subject: Re: Question on threads running in my Foundation tool
- From: Ken Thomases <email@hidden>
- Date: Sun, 22 Feb 2009 12:32:54 -0600
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@hidden)
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:
This email sent to email@hidden