How to create a run loop that only listens to performSelector:onThread: and GUI events?

Posted by Paperflyer on Stack Overflow See other posts from Stack Overflow or by Paperflyer
Published on 2010-04-13T19:43:41Z Indexed on 2010/04/13 20:23 UTC
Read the original article Hit count: 491

Filed under:
|

I want to create a separate thread that runs its own window. Frankly, the documentation does not make sense to me.

So I create an NSThread with a main function. I start the thread, create an NSAutoreleasePool, and run the run loop:

// Global:
BOOL shouldKeepRunning = YES;

- (void)threadMain {
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    // Load a nib file, set up its controllers etc.
    while (shouldKeepRunning) {
        NSAutoreleasePool *loopPool = [NSAutoreleasePool new];
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
        [loopPool drain];
    }
    [pool drain];
}

But since there is no registered port or observer, runUntilDate: exits immediately and CPU utilization goes to 100%.

All thread communication is handled by calls to performSelector:onThread:withObject:waitUntilDone:. Clearly, I am not using the API correctly. So, what am I doing wrong?

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about multithreading