1
[self request]; //main thread

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

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

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

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

4

3 回答 3

1

好的,既然你没有告诉我们你真正需要做什么,让我们猜测一下。如果您只想在后台运行选择器,请尝试 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];
    });
}
于 2012-01-14T09:39:57.170 回答
1

它可以工作。

[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");
}

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

于 2012-01-13T02:57:04.247 回答
0

regFun是在后台线程中,所以调用CFRunLoopRun()在这个线程中创建并运行一个运行循环。只有运行循环没有附加任何内容,所以它立即退出。

于 2012-01-12T13:55:15.007 回答