• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTask - working with asynchronous output with a finite termination time
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTask - working with asynchronous output with a finite termination time


  • Subject: Re: NSTask - working with asynchronous output with a finite termination time
  • From: Nir Soffer <email@hidden>
  • Date: Sun, 7 Jan 2007 17:28:20 +0200

On Jan 7, 2007, at 03:58, Matthew Brook wrote:

I've been playing with NSTask and calling a task which returns data in an asynchronous fashion, but with a finite termination time.
The output is separated by \n (newlines), and I wanted to get it into an array.


I'm very pleased with myself because it works (finally!)

So I'm sharing and hope it saves someone else a headache or two.

If you have a task which is pretty near synchronous in its output then this is overkill.
It might need its own thread in some instances eg if you want to keep a UI active while it runs.


If anyone has any brighter ideas of how to do this, or comments I'd be grateful.

Thanks

Matt

NSTask *theTask = [[NSTask alloc] init];
NSPipe *thePipe = [[NSPipe alloc]init];
NSFileHandle *resultsFileHandle = [thePipe fileHandleForReading];
NSMutableString *resultsString = [[NSMutableString alloc] init];

[theTask setLaunchPath:@"/usr/bin/whateverTheTaskNameIs"];
[theTask setStandardOutput:thePipe];
[theTask setArguments:[NSArray arrayWithObjects:someStringOrOther,anotherString,nil]];
[theTask launch];

while ([theTask isRunning])
{
NSData *theData = [resultsFileHandle availableData];

if ([theData length])
{
NSString *newData = [[NSString alloc]
initWithData:theData
encoding:NSASCIIStringEncoding];

[resultsString appendString:newData];
[newData release];
}
}

if ([theTask terminationStatus]==0)
{
NSArray *resultsArray;
resultsArray = [resultsString componentsSeparatedByString:@"\n"];
//Note that the array has a final string object which has a value of "" which needs to be ignored
CFShow(resultsArray);

} else {
printf("Problem with NSTask call\n");
return (1);
}

[thePipe release];
[theTask release];


//release other stuff

The correct way to do this is using readInBackgroundAndNotify.

See file:///Developer/ADC Reference Library/documentation/Cocoa/ Reference/Foundation/Classes/NSFileHandle_Class/Reference/ Reference.html#//apple_ref/doc/uid/20000304-DontLinkElementID_10

And file:///Developer/ADC Reference Library/documentation/Cocoa/ Reference/Foundation/Classes/NSFileHandle_Class/Reference/ Reference.html#//apple_ref/doc/uid/20000304-BCIBBBEJ

Using the asynchronous API let you do this on the main thread.


Best Regards,

Nir Soffer

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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


References: 
 >Fwd: NSTask - working with asynchronous output with a finite termination time (From: Matthew Brook <email@hidden>)

  • Prev by Date: Re: previousResponder?
  • Next by Date: Re: Strange NSView Redraw issue
  • Previous by thread: Fwd: NSTask - working with asynchronous output with a finite termination time
  • Next by thread: Re:a strange problem about the pppoe connection
  • Index(es):
    • Date
    • Thread