Reliably detecting termination of NSTask
Reliably detecting termination of NSTask
- Subject: Reliably detecting termination of NSTask
- From: John Nairn <email@hidden>
- Date: Mon, 16 Dec 2002 13:20:21 -0700
I am using code based on Apple "Moriarty" example often to launch
NSTasks and collecting their standard output. I am having a problem
that I occasionally (or frequently) do not get all the standard output
that I know should be there. I am thinking it is a problem is really
detecting NSTask termination, but it appears tricky. Here are my
current results:
1. The Moriarity method
Apple's Moriarity has a notification for standard output and
standard error. It uses a notification with zero length data as the end
of the task. Their comment says the "task is basically over." (perhaps
it is meaningful that the comment does not say "really over"). By this
approach, one of my long tasks (long scientific calculation) patiently
waits for task to finish, but when it is done there is a flurry of
standard output and this method usually drops that last few lines of
output. I therefore tried to improve termination detection.
2. My method
I extended Apple's Moriarty to have separate pipes for standard
output and standard error and separate notifications for each pipe. I
also registered a NSTaskDidTerminateNotification method. My findings
are that no method reliably detects termination, or in other words,
almost all things happen at termination. Here are the things that
happened all with high frequency:
a. NSTaskDidTerminateNotification occurs by itself
b. Empty data sent to standard output and/or standard error
followed by NSTaskDidTerminateNotification
c. Standard output continually gets sent empty data and
NSTaskDidTerminateNotification never occurs
d. Standard error continually gets sent empty data and
NSTaskDidTerminateNotification never occurs
3. My current work around, but not a solution
I currently look for any of the above (i.e., empty standard
output, empty standard error, OR NSTaskDidTerminateNotification) and
consider all to be the end of the task. This klunky method (because it
requires redundant code) always detects task completion, but does not
solve the problem of dropping last few lines in some tasks (especially
the one long task).
4. The questions
a. What is the recommended way to detect termination of an NSTask
which also guarantees that all standard output or standard error is
received by the controller?
b. Because I often have tasks that finish but never send an
NSTaskDidTerminateNotification (contrary to documentation), is there a
bug in that notification?
------------
John Nairn (1-801-581-3413, FAX: 1-801-581-4816)
Web page:
http://www.mse.utah.edu/~nairn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.