我使用NSTask,如下所示:
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/bash"];
NSMutableString *script = ... // actual script here, works fine
NSArray *args = [NSArray arrayWithObjects:@"-l",
@"-c",
script,
nil];
[task setArguments: args];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
[task launch];
NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
[task release];到目前为止运行良好,唯一的问题是,不知何故,该方法在第一次到达这一点后就不再调用了。如果我没有启动任务,一切都是正常的,所以看起来,任务以某种方式阻止了进一步的执行。
有没有人知道为什么会这样?任何提示都很感谢!
发布于 2011-10-06 07:40:30
通过运行一个循环来获得对任务的更多控制,您可以在其中读取数据,检查是否超时,等等:
while ([task isRunning])
{
NSData* data = [[pipe fileHandleForReading] availableData];
if ([data length] > 0)
{
//process the data
}
// Run current runloop in default mode; check the timeout; interrupt the task if needed
}https://stackoverflow.com/questions/7662046
复制相似问题