【问题标题】:Running multiple NSTasks consecutively连续运行多个 NSTask
【发布时间】:2011-05-14 21:34:30
【问题描述】:

我需要使用 NSTask 按顺序运行多个命令,并且想知道什么是判断任务是否完成的好方法,以便我可以继续执行下一个命令。我正在使用“sox”(我包含在我的应用程序包中)使用输入音频文件创建临时音频文件,然后需要将这些临时音频文件组合成一个文件。流程示例(不是实际的命令):

1) 歌曲A > tempA

2) 歌曲B > 温度B

3)结合 tempA tempB > songC

我正在使用以下代码来完成第一个命令:

    NSArray *arguments;
    arguments = [NSArray arrayWithObjects: @"songA", @"-f", @"-S", @"-G", @"-V", @"-b", @"24", @"-r", @"384k", @"tempA", nil];

        NSString *path=[[NSBundle mainBundle] pathForResource:@"sox" ofType:nil];

        NSTask *task;
        task = [[NSTask alloc] init];

        [task setStandardInput:[NSPipe pipe]]; 

        [task setLaunchPath:path];

        [task setArguments: arguments1];

        NSPipe *pipe;
        pipe = [NSPipe pipe];
        [task setStandardOutput: pipe];

        NSFileHandle *file;
        file = [pipe fileHandleForReading];

        [task launch];

        NSData *data;
        data = [file readDataToEndOfFile];

        NSString *string;
        string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

        NSLog (@"stuff  :\n%@", string);

        [string release];
        [task release];

假设我需要在这个进程完成之后再执行两个 NSTask 进程(使用之前进程的输出),那么检测一个进程已经完成以便我可以继续下一个进程的最佳方法是什么.

谢谢。

【问题讨论】:

    标签: objective-c cocoa macos nstask sox


    【解决方案1】:

    可能没有完全理解,但是

    [task waitUntilExit];
    

    不做这项工作?

    【讨论】:

    • 这个方法应该在哪里调用?
    • 在您希望等待任务完成的地方。在 Internet 上查找许多示例,例如cocoadev.com/wiki/NSTask
    猜你喜欢
    • 2014-10-13
    • 2012-03-13
    • 1970-01-01
    • 2020-06-08
    • 2021-12-14
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多