gpt4 book ai didi

macos - 从 NSTask 获取任务进度通知

转载 作者:行者123 更新时间:2023-12-03 16:28:42 30 4
gpt4 key购买 nike

任何人都知道在执行 NSTask 时从 NSTask 获取通知。我正在使用 NSTask 解压缩 zip 文件,并且需要在 NSProgressBar 中显示解压缩数据进度。我没有找到任何执行此类任务的想法。因此我显示进度栏中的值。需要帮助来完成此任务。提前致谢。

最佳答案

使用NSFileHandleReadCompletionNotificationNSTaskDidTerminateNotification通知。

task=[[NSTask alloc] init];

[task setLaunchPath:Path];

NSPipe *outputpipe=[[NSPipe alloc]init];

NSPipe *errorpipe=[[NSPipe alloc]init];

NSFileHandle *output,*error;

[task setArguments: arguments];

[task setStandardOutput:outputpipe];

[task setStandardError:errorpipe];

output=[outputpipe fileHandleForReading];

error=[errorpipe fileHandleForReading];

[task launch];


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedData:) name: NSFileHandleReadCompletionNotification object:output];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedError:) name: NSFileHandleReadCompletionNotification object:error];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TaskCompletion:) name: NSTaskDidTerminateNotification object:task];

//[input writeData:[NSMutableData initWithString:@"test"]];
[output readInBackgroundAndNotify];

[error readInBackgroundAndNotify];


[task waitUntilExit];

[outputpipe release];

[errorpipe release];
[task release];
[pool release];


/* Called when there is some data in the output pipe */

-(void) receivedData:(NSNotification*) rec_not

{

NSData *dataOutput=[[rec_not userInfo] objectForKey:NSFileHandleNotificationDataItem];

[[rec_not object] readInBackgroundAndNotify];

[strfromdata release];

}

/* Called when there is some data in the error pipe */

-(void) receivedError:(NSNotification*) rec_not

{
NSData *dataOutput=[[rec_not userInfo] objectForKey:NSFileHandleNotificationDataItem];

if( !dataOutput)

NSLog(@">>>>>>>>>>>>>>Empty Data");

[[rec_not object] readInBackgroundAndNotify];


}

/* Called when the task is complete */

-(void) TaskCompletion :(NSNotification*) rec_not

{

}

关于macos - 从 NSTask 获取任务进度通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17788267/

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