我对如何做到这一点有点不确定:
我启动了一个“工作线程”,它在我的应用程序“生命”期间运行。
[NSThread detachNewThreadSelector:@selector(updateModel) toTarget:self withObject:nil];
然后
- (void) updateModel {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
BackgroundUpdate *update = [[BackgroundUpdate alloc] initWithTimerInterval:5];
[[NSRunLoop currentRunLoop] run]; //keeps it going 'forever'
[update release];
[pool release];
}
现在线程每5秒唤醒一次(initWithTimerInterval)以查看是否 它可以做任何任务。 BackGroundUpdate类中的所有任务目前仅依赖于时间。我想有一些“事件依赖”。例如我想从我的主线程中调用Background对象并告诉它“speedUp”,“slowDown”,“reset”或对象上的任何方法。
要做到这一点,我想我需要像performSelectorOnThread
这样的东西,但是如何获得对NSthread和背景对象的引用?
答案 0 :(得分:3)
直接回答:使用[[NSThread alloc] initWithTarget:selector:object:]而不是+ [NSThread detachNewThreadSelector:toTarget:withObject:]。别忘了打电话给-start!
其他想法: