为什么 CFRunLoopRun 不起作用?

发布于 2024-12-26 03:43:36 字数 325 浏览 0 评论 0原文

[self request]; //main thread

- (void)request {
    [self performSelectorInBackground:@selector(regFun) withObject:nil];
}

- (void)regFun {
    CFRunLoopRun();
    CCLOG(@"CFRunLoopRun not work");
}

鉴于前面的代码,您知道为什么 CFRunLoopRun() 不起作用吗?我需要在后台调用 regFun 。

还有其他方法可以停止后台线程吗?

[self request]; //main thread

- (void)request {
    [self performSelectorInBackground:@selector(regFun) withObject:nil];
}

- (void)regFun {
    CFRunLoopRun();
    CCLOG(@"CFRunLoopRun not work");
}

Given the previous code, do you know why CFRunLoopRun() is not working?. I need to call regFun in background.

Are there any other ways to stop background thread?

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

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

发布评论

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

评论(3

九局 2025-01-02 03:43:36

它可以工作。

[self request]; //main thread

- (void)request {
    //[self performSelectorInBackground:@selector(regFun) withObject:nil];
    [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(regFun) userInfo:nil repeats:NO];
}

- (void)regFun {
    CFRunLoopRun();
    CCLOG(@"CFRunLoopRun not work");
}

但我不确定这是否是正确的方法,我也不知道发生了什么。 :(

It can work.

[self request]; //main thread

- (void)request {
    //[self performSelectorInBackground:@selector(regFun) withObject:nil];
    [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(regFun) userInfo:nil repeats:NO];
}

- (void)regFun {
    CFRunLoopRun();
    CCLOG(@"CFRunLoopRun not work");
}

But I'm not sure this is the right approach, and I don't know what happened. :(

几味少女 2025-01-02 03:43:36

好吧,既然你没有告诉我们你真正需要做什么,让我们猜猜。如果您只想在后台运行选择器,请尝试 Grand Central Dispatch:

- (void) request {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self regFun];
    });
}

- (void) regFun {
    // Running in background, with a run loop available
    // and an autorelease pool, too. After the code finishes:
    dispatch_async(dispatch_get_main_queue(), ^{
        // Will be called on the main thread.
        [self reportBackgroundTaskFinished];
    });
}

OK, since you are not telling us what you really need to do, let’s guess. If you just want to run a selector in the background, try Grand Central Dispatch:

- (void) request {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self regFun];
    });
}

- (void) regFun {
    // Running in background, with a run loop available
    // and an autorelease pool, too. After the code finishes:
    dispatch_async(dispatch_get_main_queue(), ^{
        // Will be called on the main thread.
        [self reportBackgroundTaskFinished];
    });
}
流星番茄 2025-01-02 03:43:36

regFun 位于后台线程中,因此对 CFRunLoopRun() 的调用在此线程中创建并运行运行循环。只是运行循环没有附加任何内容,因此它会立即退出。

regFun is in a background thread, so the call to CFRunLoopRun() creates and runs a run loop in this thread. Only there's nothing attached to the run loop, so it exits immediately.

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