Monday, May 9, 2011

NSThread isCancelled example objc

[An NSThread isCancelled method example]

Create a NSThread object with following code fragment.


NSThread *newThread = [[NSThread alloc] initWithTarget:self selector:@selector(MyThread:) object:timer];

To stop this thread externally, you can call NSThread:cancel method at some
point and make your main thread method to check for whether it's cancelled or not
by calling NSThread isCancelled method.

In your main thread method, you should check whether it's cancelled or not periodically.


// At some checkpoint
if([[NSThread currentThread] isCancelled]) {
    /* do some clean up here */
    [NSThread exit];
}