因此,我开始一个新的NSThread,我希望能够稍后使用通过调用performSelector:onThread:...
。根据我的理解,它调用方法将该调用添加到该线程上的runloop,因此在下一次迭代时,它将弹出所有这些调用,并随后调用它们,直到没有任何剩余的调用。所以我需要这种功能,一个空闲的线程准备工作,我可以调用它。我目前的代码如下所示:保持NSThread活着,并运行它的NSRunLoop
- (void)doInitialize
{
mThread = [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil];
[mthread start];
}
- (void)runThread
{
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
// From what I understand from the Google machine is that this call should start the
// runloop on this thread, but it DOESN'T. The thread dies and is un-callable
[[NSRunLoop currentRunLoop] run];
[pool drain];
}
- (void)scheduleSomethingOnThread
{
[self performSelector:@selector(hardWork) onThread:mThread withObject:nil waitUntilDone:NO];
}
但是线程不保持活动状态,并且performSelector:onThread不执行任何操作。我如何以正确的方式去做这件事?
谢谢,这是一个有点清晰了。 – ErikPerik 2011-06-08 01:43:31
这对命令行工具中的主运行循环也是如此。它是NS/UIApplication,它在GUI应用程序的主运行循环中提供默认输入源。 – 2011-06-08 02:13:51