Rohit Singhal
Rohit Singhal

Reputation: 381

How to stop the execution of a thread in iphone

I want to stop the execution of the thread.I used "iscancelled" but there is something going wrong..After cancelling the thread it's execution does not stop.

   NSLog(@"YES-------%d,%d", [myThread isExecuting], [myThread isCancelled]);//(OutPut--1,0)
   [myThread cancel];
if([[NSThread currentThread] isCancelled]) 
     {
    [NSThread exit];
 }
   NSLog(@"YES-------%d,%d", [myThread isExecuting], [myThread isCancelled]);//(OutPut--1,1)

How is it possible that after canceling the thread it's execution does not stop????

Thanks,

Upvotes: 1

Views: 9705

Answers (2)

Michael Dautermann
Michael Dautermann

Reputation: 89509

I'm pretty certain [myThread cancel] is meant to signal another thread in your program to cancel.

If you want to cancel the current thread, just do [NSThread exit];

Upvotes: 3

Saurabh
Saurabh

Reputation: 22873

use this in your thread to stop itself -

[NSThread exit];

you can also use cancel method check out this - http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSThread_Class/Reference/Reference.html

Upvotes: 2

Related Questions