简体   繁体   English

从NSTask获取任务进度的通知

[英]Get Notification of task progress from NSTask

Any body have idea about getting notification from NSTask while NSTask is executed. 任何人都有在执行NSTask时从NSTask接收通知的想法。 I am unzipping a zip file using NSTask and need to show the unzip data progress in a NSProgressBar. 我正在使用NSTask解压缩zip文件,并且需要在NSProgressBar中显示解压缩数据的进度。 I don't found any idea for doing such task.So that i show the value in progress bar. 我没有发现执行此任务的任何想法。因此,我在进度栏中显示了该值。 Need help for doing this task. 需要帮助来完成此任务。 Thanks in advance. 提前致谢。

Use NSFileHandleReadCompletionNotification , NSTaskDidTerminateNotification notifications. 使用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

{   

}

In order to show progress, you need to find out two things: 为了显示进度,您需要找出两件事:

  • How many files there are in the archive, or how many bytes they will occupy after unzipping is complete 档案中有多少个文件,或者解压缩完成后它们将占用多少字节
  • How many files or bytes you have unzipped so far 到目前为止,您已解压缩了多少个文件或字节

You'll find these out by reading the output from the unzip task. 通过阅读解压缩任务的输出,可以找到这些内容。 Parag Bafna's answer is a start; 帕拉格·巴夫纳(Parag Bafna)的答案是一个开始。 in receivedData: , you'll need to parse the output to determine what progress has just happened, and then add that progress to your running count of progress so far (eg, ++_filesUnzippedSoFar ). receivedData: ,您将需要解析输出以确定刚刚发生了什么进度,然后将该进度添加到到目前为止的运行进度计数中(例如++_filesUnzippedSoFar )。

The first part, finding out the total size of the job, is trickier. 第一部分,找出工作的总规模,比较棘手。 You basically need to run unzip before you run unzip: the first, with -l (that's a lowercase L), is to List the contents of the archive; 基本上,您需要先运行解压缩,然后再运行解压缩:第一个带有-l (即小写的L)的名称是列出归档文件的内容。 the second is to unzip it. 第二个是解压缩它。 The first one, you read the output to determine how many files/bytes the archive contains; 第一个,您读取输出以确定归档文件包含多少文件/字节。 the second one, you read the output to determine the value to advance the progress bar to. 在第二个中,您将读取输出,以确定将进度条前进到的值。

Setting the properties of the progress bar is the easy part; 设置进度条的属性很容易。 those are literally just doubleValue and maxValue . 这些实际上只是doubleValuemaxValue Working out where you are in the job is the hard part, and is very domain-specific—you need to read unzip's output (twice, in different forms), understand what it's telling you, and translate that into progress information. 确定您在工作中的位置是困难的部分,并且是特定于领域的-您需要阅读unzip的输出(两次,以不同的形式),了解它告诉您的内容,并将其转换为进度信息。

There is nothing in NSTask that can help you with that. NSTask中没有什么可以帮助您的。 NSTask's part of this begins and ends at the standardOutput property. NSTask的这一部分从standardOutput属性开始和结束。 It has no knowledge of zip files, archives, contents of archives, or even progress, since none of that applies to most tasks. 它不了解zip文件,档案,档案内容甚至进度,因为这些都不适用于大多数任务。 It's all specific to your task, which means you have to write the code to do it. 所有这些都是针对您的任务的,这意味着必须编写代码才能执行此任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM