如何创建一个仅由 PerformSelector... 方法调用启动的运行循环?

发布于 2024-08-09 14:11:43 字数 379 浏览 5 评论 0原文

我正在搞乱一些线程。现在考虑一下: 我有一个主线程。我开始一个新线程。在它的入口点方法中,我想创建一个运行循环。现在文档告诉我我必须有一个输入源。否则我的运行循环会立即退出。坏的。好的。但除了我的 PerformSelector... 方法调用之外,我没有其他输入源。线程启动后,会出现一个 PerformSelector 方法,该方法将在延迟一段时间后启动该线程上的另一个方法。在该方法内会发生另一个执行选择器调用,依此类推。每个延迟在 0.1 到 1 秒之间。因此,重复触发计时器是毫无意义的;-)

我如何设置该运行循环,使其保持活动状态以接收来自 PerformSelector 的踢动?我希望线程在无事可做时休眠。但是当performSelector踢到他屁股上时,我希望线程醒来并工作。

有什么建议吗?

I'm messing around with threads a little bit. Now consider this:
I have a main thread. I start a new thread. In it's entry-point method, I want to make a run loop. Now the documentation tells meh that I have to have a input source. Otherwise my run loop exits immediately. bad. okay. but I have no other input source than my performSelector... method calls. After the thread is started, there comes a performSelector method that will kick in another method on that thread after some delay. inside that method another performSelector call happens, and so on. each with a delay between 0.1 and 1 sec. So a repeatedly firing timer is senseless right ;-)

How could I set up that run loop so it keeps alive to receive kicks from performSelector? I want the thread to sleep when there's nothing to do. but when a performSelector kick is comming in his butt, I want that the thread wakes up and works.

Any suggestions, anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙沙粒小 2024-08-16 14:11:43

您想要的代码在 运行循环。但它被隐藏在其他讨论中,如果您不理解本页上的其他所有内容,您将不太知道自己在看什么。阅读该部分,然后这段代码将有望有意义:

- (void)startRunLoop:(id)sender
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Any thread setup

    do
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                 beforeDate:[NSDate distantFuture]];
    } while (self.isStarted);

    // Any thread cleanup

    [pool release];
}

The code you want is explained in Figure 3-14 in Run Loops in the Threading Programming Guide. But it's buried so well in other discussion that if you don't understand everything else on this page, you won't quite know what you're looking at. Read that section, and then this code will hopefully make sense:

- (void)startRunLoop:(id)sender
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Any thread setup

    do
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                 beforeDate:[NSDate distantFuture]];
    } while (self.isStarted);

    // Any thread cleanup

    [pool release];
}
怪我太投入 2024-08-16 14:11:43

你没有描述你真正想做的事情,所以很难说,但听起来你把事情变得有点过于复杂了。

我相信你只想有一个线程(以你喜欢的任何方式启动),并且该线程应该使用 NSCondition/NSLock 来休眠,直到你希望它醒来。

请参阅此SO thread 对于类似的问题和解释如何做到这一点的好答案:

You don't describe what you're REALLY trying to do, so it's difficult to tell, but it sounds like you're overcomplicating things a bit.

I believe that you want to have just one thread (kicked off in whatever way you like) and that thread should make use of NSCondition/NSLock to sleep until you want it to wake up.

See this S.O. thread for a similar question and a good answer explaining how to do it:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文