Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTask waitForDataInBackgroundAndNotify and waitUntilExit




On May 12, 2008, at 23:04, Shawn Erickson wrote:

Your placement of waitUntilExit is blocking the runloop which needs to
be running to service the background notifications you requested.

Ah ...I see. That makes sense.

If
you want async behavior then you cannot wait on the task (at least not
in the way you are currently attempting).

Review the following code example...

http://developer.apple.com/samplecode/Moriarity/listing5.html

Thanks for the pointer.

Well, actually I was just using waitForDataInBackgroundAndNotify trying to understand the options.

Looking at http://www.cocoadev.com/index.pl?NSTaskWaitUntilExit I would have thought this should work

	NSTask *task = [[NSTask alloc] init];
	[task setLaunchPath:path];
	[task setArguments:args];
	
	NSPipe *outPipe = [NSPipe pipe];
	[task setStandardOutput:outPipe];
	NSFileHandle *outFile = [outPipe fileHandleForReading];

	NSPipe *errPipe = [NSPipe pipe];
	[task setStandardError:errPipe];
	NSFileHandle *errFile = [errPipe fileHandleForReading];	

	[task launch];

    NSData *data;

while((data = [outFile availableData]) && [data length]) {
[output appendString:[NSString stringWithCString:[data bytes] encoding: NSUTF8StringEncoding]];
}


while((data = [errFile availableData]) && [data length]) {
[error appendString:[NSString stringWithCString:[data bytes] encoding: NSUTF8StringEncoding]];
}


	[task waitUntilExit];

While it works just fine for executing "ps" even the most simple shell script:

    #!/bin/sh
    echo "out"
    echo "err" >&2
    exit 0

gets stuck. No matter whether I call it directly or via "/bin/sh -c pathtoscript". The first call to "availableData" just blocks. Looking at the documentation ("if no data is available, the method blocks") I am a little surprised as there should be some data available.

Any further suggestions or comments?

cheers
--
Torsten
_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >NSTask waitForDataInBackgroundAndNotify and waitUntilExit (From: Torsten Curdt <email@hidden>)
 >Re: NSTask waitForDataInBackgroundAndNotify and waitUntilExit (From: "Shawn Erickson" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.