注册一个NSFileHandleReadToEndOfFileCompletionNotification

问题描述:

你好样的女士们,先生们:)注册一个NSFileHandleReadToEndOfFileCompletionNotification

我试图找出如何使这个piece of code from a previous question工作,但我卡在了如何“注册”一NSFileHandleReadToEndOfFileCompletionNotification的部分。

这是我的代码:

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]; 

...它运行正常,直到我补充一点:

[[outputPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify]; 

...这会导致程序冻结。当我补充一点:

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

...代码不编译,我得到了在这个问题上警告

'notification' undeclared. 

任何援助的收入代表了我大量丰富的感激之情!

运行top在命令行中使用这些参数将导致它不断打印统计信息,并且它永远不会写入EOF。这就是为什么-readToEndOfFileInBackgroundAndNotify永远运行。

这听起来像你可能只是想读第一次迭代,然后杀死任务。

至于错误,它听起来像你没有一个变量名为notification包含该行的方法。

+0

不太“从不”;它会打印出统计数据一小时(3600秒),然后退出并结束文件。 (当然,当应用程序被冻结的时候,这对用户没有任何影响。) – 2010-08-25 15:00:02

+0

啊,是的,谢谢。我并不完全熟悉“顶”的所有参数。 – kperryua 2010-08-25 16:03:05