views:

13

answers:

1

Hello kind ladies and gentlemen :)

I'm trying to figure out how to make this piece of code from a previous question work, but I'm stuck on the part on how to 'register' a NSFileHandleReadToEndOfFileCompletionNotification.

This is my code:

NSTask *topTask = [NSTask new];
[topTask setLaunchPath:@"/usr/bin/top"];
[topTask setArguments:[NSArray arrayWithObjects:@"-s", @"1", @"-l", @"3600", @"-stats", @"pid,cpu,time,command", nil]];

NSPipe *outputPipe = [NSPipe pipe];
[topTask setStandardOutput:outputPipe];
[topTask launch];

... which runs fine until I add this:

[[outputPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];

... which causes the program to freeze. And when I add this:

NSString *outputString = [[[NSString alloc] initWithData:[[notification userInfo] objectForKey:NSFileHandleNotificationDataItem] encoding:NSUTF8StringEncoding] autorelease];

... the code doesn't compile and I get the !warning

'notification' undeclared. 

Any assistance on this matter earns copious amounts of gratitude on my behalf.

+2  A: 

Running top with those parameters in the command line will cause it to continually print out stats and it will never write EOF. That's why -readToEndOfFileInBackgroundAndNotify runs forever.

It sounds like you may just want to read the first iteration and then kill the task.

As for the error, it sounds like you simply don't have a variable named notification in the method containing that line.

kperryua
Not quite “never”; it will print out stats for one hour (3600 seconds), then exit and thereby end the file. (Of course, when the app is frozen for that time, this makes no difference to the user.)
Peter Hosey
Ah yes, thanks. I wasn't completely familiar with all of those parameters to `top`.
kperryua