tags:

views:

83

answers:

1

Hi,

I want to run an unknown amount (unknown at compile time) of NSTasks and I want to run an unknown amount (again, at compile time, max. 8) of them simultaneously. So basically I loop through a list of files, generate an NSTask, run it until the maximum of simultaneous tasks are ran and whenever one finishes another NSTask starts until all of them are done.

My approach would be creating a class that generates an NSTask and subclass it to change parameters here and there when there's a different input (changes that are made from the interface). Then the superclass will run the NSTask and will have an @synthesize method returning its progress. Those objects will be generated in the above repeat loop and the progress will be displayed.

Is this a good way to go? If so, can someone give me a quick example of how the repeat loop would look like? I don't know how I would reference to all objects once they're run.

for (; !done ;) {
    if (maxValue ≥ currentValue) {
  //Run Object with next file.
  //Set currentValue.
 }
 //display progress and set done to YES if needed and set currentValue to it -1 if needed
}

Thanks in advance.

+2  A: 

There's no loop exactly.

Create an array for tasks not yet started, another with tasks that are running, and another with tasks that have finished. Have a method that pulls one task from the pending-tasks array, starts (launches) it, and adds it to the running-tasks array. After creating the arrays and filling out the pending-tasks array, call that method eight times.

When a task finishes, remove the task from the running-tasks array and add it to the finished-tasks array, then check whether there are any tasks yet to run. If there's at least one, call the run-another-one method again. Otherwise, check whether there are any still running: If not, all tasks have finished, and you can assemble the results now (if you haven't been displaying them live).

Peter Hosey
Thanks, but if there's no repeat loop, then how would I update the progress live?
dave-gennel: You would do that in your NSTaskDidTerminateNotification handler method, and/or upon receiving output from one of the tasks.
Peter Hosey
I hate to ask it again, but how would I live update my interface? I would loop until an action is done to read its output and generate the percentage done. Should I create a loop in the object that does that for me? Or what would you recommend (Sorry for asking so much questions)
No, there is no loop. See my previous comment.
Peter Hosey
Okay, first I didn't know what you meant with "receiving output from one of the tasks" but it looks like I can receive live updates from it so that should do it. Thank you :)