tags:

views:

415

answers:

3

How can NSNotification be used to check a the status of an NSTask? I know there are a couple of class methods inside the NSTask but don't really understand how to implement them in a Cocoa application. Can somebody help me?

+5  A: 

This blog post, Better way to read from an NSTask, describes how to use NSNotification to recieve notifications from an NSTask.

If you know that your task is not long-running, there is a simpler solution. My answer to another SO question, Execute a terminal command from a Cocoa app, has an example of using NSTask that includes returning the status of the task w/o NSNotification

Gordon Wilson
A: 

thanks Gordon,i have tried out the solution u given but it confusing me is when i write the code: NSPipe *pipe; pipe = [NSPipe pipe]; [task setStandardOutput: pipe];

NSFileHandle *file; file = [pipe fileHandleForReading];

[task launch];

NSData *data; data = [file readDataToEndOfFile];

what is the output data can be return from the task?for my case the console window jus get stuck there and nothing have returned

A: 

Task will be stuck when you won't read what it is sending on stdout/stderr. That's why you need to create pipes and set them using setStandardOutput.

Use [NSFileHandle readToEndOfFileInBackgroundAndNotify] and wait for NSFileHandleReadToEndOfFileCompletionNotification. It will give you all data in the callback.

porneL