您在NSThread
子类中检查isCancelled
。您在NSThread
子类中的整个代码中检查isCancelled
。当您致电cancel
时,您的NSThread
子类继续运行,直到它遇到isCancelled
的检查。你所做的是将isCancelled
检查多个地方希望当你打电话cancel
它击中isCancelled
检查并尽快退出。
从你的示例代码,你贴我改变了TimerThread.m
看起来像这样和正常工作:
#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
NSThread在主函数返回后终止。 – CouchDeveloper
@CouchDeveloper我想在此之前终止。 –
您必须从'main' *过早返回的唯一选项*已被@random完全描述。 – CouchDeveloper