6

我正在使用 NSTask,但是当我启动任务时,它会阻塞主线程(所以我无法更新它),直到任务结束。这是我的代码:

NSString *hostsforping = @"google.es";
    pingdata = [[NSTask alloc] init];
    [pingdata setLaunchPath: @"/sbin/ping"];

    NSArray *pingargs;
    pingargs = [NSArray arrayWithObjects: @"-c 5", hostsforping, nil];
    [pingdata setArguments: pingargs];

    NSPipe *pingpipe;
    pingpipe = [NSPipe pipe];
    [pingdata setStandardOutput: pingpipe];

    NSFileHandle *pingfile;
    pingfile = [pingpipe fileHandleForReading];

    [pingdata launch];

    NSData *pingdata1;
    pingdata1 = [pingfile readDataToEndOfFile];

    NSString *pingstring;
    pingstring = [[NSString alloc] initWithData: pingdata1 encoding: NSUTF8StringEncoding];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(taskDidTerminate:)
                                                 name:NSTaskDidTerminateNotification
                                               object:nil];
}
- (void) taskDidTerminate:(NSNotification *)notification {
    NSLog(@"end");
}

我一直在阅读-waitUntilExit确实阻塞了主线程,但我没有使用它,所以我不知道我做错了什么。

4

2 回答 2

11

在后台线程上运行任务,readDataToEndOfFile阻塞主线程。

// Offload the method onto a background thread, could also use Grand Central Dispatch   
[self performSelectorInBackground:@selector(startTask) withObject:nil];


- (void)startTask {
    NSString *hostsforping = @"google.es";
    NSTask *pingdata = [[NSTask alloc] init];
    [pingdata setLaunchPath: @"/sbin/ping"];

    NSArray *pingargs;
    pingargs = [NSArray arrayWithObjects: @"-c 5", hostsforping, nil];
    [pingdata setArguments: pingargs];

    NSPipe *pingpipe;
    pingpipe = [NSPipe pipe];
    [pingdata setStandardOutput: pingpipe];

    NSFileHandle *pingfile;
    pingfile = [pingpipe fileHandleForReading];

    [pingdata launch];

    NSData *pingdata1;    
    pingdata1 = [pingfile readDataToEndOfFile];

    NSString *pingstring;
    pingstring = [[NSString alloc] initWithData: pingdata1 encoding: NSUTF8StringEncoding];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(taskDidTerminate:)
                                                 name:NSTaskDidTerminateNotification
                                               object:nil];
}

- (void) taskDidTerminate:(NSNotification *)notification {
    // Note this is called from the background thread, don't update the UI here
    NSLog(@"end");

    // Call updateUI method on main thread to update the user interface
    [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];
}
于 2011-10-06T15:37:04.020 回答
1

由于您使用 readDataToEndOfFile,因此您隐含地说您的 NSTask 需要阻塞执行。我可以建议您使用 dispatch_async 将代码包装在 GCD 括号中。例如:

- (void)executeShellScript {
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSString *execPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myScript.sh"];

        NSPipe *pipe = [NSPipe pipe];
        NSFileHandle *file = pipe.fileHandleForReading;

        NSTask *task = [[NSTask alloc] init];
        task.launchPath = @"/bin/sh";
        task.arguments = @[execPath];
        task.standardOutput = pipe;

        [task launch];

        NSData *data = [file readDataToEndOfFile]; //readDataToEndOfFile is blocking the main thread.
        [file closeFile];
        NSString *bashOutput = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

        NSLog (@"shell returned:\n%@", bashOutput);
    });
}
于 2015-08-26T21:34:05.980 回答