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?
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With