Satish
Satish

Reputation: 133

How to stop a task initiated using NSOperationQueue?

enter image description hereI have created a process using NSOperationQueue in the below way:

queue = [[NSOperationQueue alloc]init];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self 
                                                                           selector:@selector(loadDataWithOperation)object:nil];
[queue addOperation:operation];
[operation release];

In the loadDataWithOperation method I wrote code for printing infinite numbers.

Now I have called cancelAppOperations to stop its execution and also I have tried to set suspended value to YES like this

[queue setSuspended:YES]

even though the process is still running, I am calling the stoping method by using "stop"button. It is calling in the same class.

How can I stop it?

Upvotes: 0

Views: 1263

Answers (2)

Satish
Satish

Reputation: 133

Here for suspend of queue [queue setSuspended:YES] is working fine,there I check the condition for stop the For loop i.e

if(![queue isSuspended]) 
{
        NSLog(@"elements are :%d",i);
}
else
{
    break; 
}

I placed these lines of code in "loadDataWithOperation" method

Upvotes: 0

gheese
gheese

Reputation: 1210

You can send a cancellation message to all operations in the queue before they begin executing, but if your operation is already executing you need to process the cancel message

Upvotes: 1

Related Questions