I created a sub-thread using NSThread in main thread
NSThread *newThread = [[NSThread alloc] initWithTarget:self selector:@selector(MyThread:) object:timer];
5 sec later,i used [newThread cancel] in main thread to stop the sub-thread,but it didnt work,
Method MyThread: in newThread still working
so,whats the correct answer to stop newThread,THX
actually [newThread isCancelled] is YES,but selector MyThread was still woking
The
cancelmethod only informs the thread that it is cancelled (as you mentioned changes theisCancelledtoYES. It’s then the responsibility of the thread itself to check this and exit. For example, in yourMyThread:method you could do this:You should do this check periodically, and exit from within the thread as shown; otherwise the
canceldoesn’t have any effect.