本文介绍了从NSTask获取任务进度的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有在执行NSTask时从NSTask接收通知的想法.我正在使用NSTask解压缩zip文件,并且需要在NSProgressBar中显示解压缩数据的进度.我没有发现执行此任务的任何想法.因此,我在进度栏中显示了该值.需要帮助来完成此任务.预先感谢.

Any body have idea about getting notification from NSTask while NSTask is executed. I am unzipping a zip file using NSTask and need to show the unzip data progress in a 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.

推荐答案

使用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

{

}

这篇关于从NSTask获取任务进度的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 10:08