1
[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?

yeforriak
  • 1,705
  • 2
  • 18
  • 26
wupei
  • 21
  • 3
  • 2
    What exactly are you trying to do? Most probably there’s an easier way around. – zoul Jan 12 '12 at 11:31
  • 1
    Check [the docs](http://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFRunLoopRef/Reference/reference.html#//apple_ref/doc/uid/20001441-CH201-F17704). – Georg Fritzsche Jan 12 '12 at 11:42

3 Answers3

1

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. :(

wupei
  • 21
  • 3
1

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];
    });
}
zoul
  • 102,279
  • 44
  • 260
  • 354
0

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.

joerick
  • 16,078
  • 4
  • 53
  • 57
  • I do not understand. If I do this. CFRunLoopRun() can work. [self performSelector:@selector(regFun)]; But I need to call regFun in background. Because I hope that main thread continue running. – wupei Jan 12 '12 at 14:50
  • what do you mean there's nothing attached to the run loop? Shouldn't it run even though there are no pending operations? e.g. waiting for new operations? – paiego Mar 13 '12 at 09:55
  • @paiego no, that's not how it works. The run loop will exit if there's nothing attached to it. You can attach a timer to it and it will remain running indefinitely. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html#//apple_ref/doc/uid/20000321-CHDEBABE – joerick Mar 13 '12 at 22:44