保留对 NSThread 的引用并向其对象发送消息?
我有点不确定如何执行此操作:
我启动一个“工作线程”,该线程在我的应用程序“生命周期”期间运行。
[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 类中的所有任务目前仅与时间相关。我想要一些“事件相关”的。例如,我想从主线程调用后台对象,并告诉它“speedUp”、“slowDown”、“reset”或对象上的任何方法。
为此,我想我需要类似 performSelectorOnThread
的东西,但是如何获取对 NSthread 和背景对象的引用?
I am a bit uncertain on how to do this:
I start a "worker-thread" that runs for the duration of my apps "life".
[NSThread detachNewThreadSelector:@selector(updateModel) toTarget:self withObject:nil];
then
- (void) updateModel {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
BackgroundUpdate *update = [[BackgroundUpdate alloc] initWithTimerInterval:5];
[[NSRunLoop currentRunLoop] run]; //keeps it going 'forever'
[update release];
[pool release];
}
Now the thread "wakes" up every 5 seconds(initWithTimerInterval) to see if
there are any tasks it can do. All the tasks in the BackGroundUpdate Class are only time dependent for now. I would like to have a few that were "event dependent". e.g. I would like to call the Background Object from my main thread and tell it to "speedUp", "slowDown", "reset" or any method on the object.
To do this I guess I need something like performSelectorOnThread
but how to get a reference to the NSthread and the Background Object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直接回答:不要使用+[NSThread detachNewThreadSelector:toTarget:withObject:],而是使用[[NSThread alloc] initWithTarget:selector:object:]。不要忘记拨打-start!
其他想法:
Direct answer: Instead of +[NSThread detachNewThreadSelector:toTarget:withObject:], use [[NSThread alloc] initWithTarget:selector:object:]. Don't forget to call -start!
Other thoughts: