You check for isCancelled
in your NSThread
subclass. You check for isCancelled
throughout your code in NSThread
subclass. When you call cancel
, your NSThread
subclass continues to run until it hits a check for isCancelled
. What you do is place the isCancelled
check multiple places in hopes when you call cancel
it hits a isCancelled
check and exits as soon as possible.
From your example code you posted I changed the TimerThread.m
to look like this and it works fine:
#import "TimerThread.h"
#import "Giraffe.h"
@interface TimerThread () {
Giraffe *giraffe;
}
@end
@implementation TimerThread
- (void)main {
if (self.isCancelled)
return;
giraffe = [[Giraffe alloc] init];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(calculate:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] run];
}
- (void)calculate:(NSTimer*)timer {
if (self.isCancelled) {
[timer invalidate];
return;
}
[giraffe calculateValues:timer];
}
@end