开发者

Registering for a NSFileHandleReadToEndOfFileCompletionNotification

I'm trying to figure out how to make this p开发者_JAVA百科iece 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜