Q:

How to pause/continue NSThread

I have an app, where i use function FSMoveObjectToTrashSync. It works in background thread. I need ability for my app, to click on button to pause it or continue(if it paused) how i can make it?
Example of code:
NSMutableArray *fileArray = [NSMutableArray array withobjects:@"file1url", @"file2", @"file3", nil]; 
NSMutableArray *threadArray = [[NSMutableArray alloc] init];

-(void)myFunc{
   for (NSURL *url in fileArray){
        NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(mySelectorWith:) object:url];
        [thread start];
        [threadArray addObject:thread];
   }
}

-(void)mySelectorWith:(NSURL *) url{
       FSRef source;
       FSPathMakeRef((const UInt8 *)[[url path] fileSystemRepresentation], &source, NULL);
       FSMoveObjectToTrashSync(&source, NULL, kFSFileOperationDefaultOptions);
}

PS:sorry for my english, i'm from Belarus... =(

A:

One solution would be to replace the for loop on a single thread with an NSOperation subclass. Each operation should trash exactly one object; you then create one operation for each object you want to trash and put all of the operations on an NSOperationQueue.
The operation queue will run each operation on a thread, and it can even run multiple operations on multiple threads if it sees enough computing power laying around to do it.
An operation queue can be paused and resumed at will; when you suspend the queue, any operations in that queue that are already running will finish, but no more will start until you resume the queue.

