gpt4 book ai didi

cocoa - 使用 NSTask 交互式访问命令

转载 作者:行者123 更新时间:2023-12-03 17:09:38 25 4
gpt4 key购买 nike

我正在使用 NSTask 来运行交互式的 cli 应用程序。我用它通过 USB 连接提取数据。

我正在打开一个带有大纲和错误管道的 NSTask,但是当我发出命令时,它可以工作,但会在循环中旋转沙滩球,从输出中提取数据。

我希望能够混合执行帮助命令的 - (IBAction)clicked:(id)sender 按钮并获取输出:

NSTask *usbCommandTask;
NSPipe *outPipe;
NSPipe *inPipe;
NSPipe *errorPipe;
NSFileHandle *inFile;
NSFileHandle *outFile;
NSTimer *pollTimer;
dispatch_queue_t mtpTask;

@implementation AppDelegate


- (void)commandNotification:(NSNotification *)notification
{
NSData *data = nil;

while ((data = [outFile availableData]) && [data length]){
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data: %@",myString);
}
NSLog(@"Execution never gets here");

}

-(void)checkTask
{
if(usbCommandTask){
if([usbCommandTask isRunning])NSLog(@"Task running"); else NSLog(@"Task dead");

} else NSLog(@"Task is nil");


}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {



usbCommandTask = [[NSTask alloc] init];
[usbCommandTask setLaunchPath:@"/Applications/usb-debugger-cli"];
[usbCommandTask setCurrentDirectoryPath:@"/"];
[usbCommandTask setArguments:[NSArray arrayWithObject:@"-i"]];
inPipe = [NSPipe pipe];
[usbCommandTask setStandardInput:inPipe];




outPipe = [NSPipe pipe];
[usbCommandTask setStandardOutput:outPipe];
errorPipe = [NSPipe pipe];
[usbCommandTask setStandardError:errorPipe];

outFile = [outPipe fileHandleForReading];
inFile = [inPipe fileHandleForWriting];

[outFile waitForDataInBackgroundAndNotify];



[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(commandNotification:)
name:NSFileHandleDataAvailableNotification
object:nil];



pollTimer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(checkTask) userInfo:nil repeats:TRUE];
[usbCommandTask launch];
NSLog(@"Launched");



}


- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
if(usbCommandTask)[usbCommandTask terminate];


}


- (IBAction)clicked:(id)sender {

if(usbCommandTask){
NSString *command=@"help\n";
NSData *commandData=[NSData dataWithBytes:command.UTF8String length:command.length];

[inFile writeData:commandData];
}
else
{NSLog(@"Comamnd task dead");
}
}

最佳答案

[outFile availableData] 

实际上是一个阻塞调用,没有数据或显式 EOF,所以这将始终阻塞,而不是我使用:

data = [outFile availableData];
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data: %@",myString);
[outFile waitForDataInBackgroundAndNotify];

关于cocoa - 使用 NSTask 交互式访问命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42772624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com