On Dec 3, 2008, at 11:24 PM, Michael Ash wrote:
On Wed, Dec 3, 2008 at 11:11 PM, Sean McBride <[EMAIL PROTECTED]> wrote:You said you wanted your thread to 'patiently wait forperformSelector:onThread: calls'? When do you want it to stop waiting? If at app quit time, then just leave it around. If earlier, then justsend it a message to clean itself up (dealloc memory, etc.) and invalidate the timer. Then you are back to having no runloop sources and voila.Don't do this. When the docs say that your runloop will exit when no sources are installed on the runloop, this is a warning, not a guide for how to make it exit. The system may install its own sources and not remove them just because you want to exit, in which case your thread will run forever. Worse, this may happen on an OS release later than the one you tested with, causing your app to leak threads after you've already shipped it. The best way would probably be to do something like this: NSDate *distantFuture = [NSDate distantFuture]; NSThread *myThread = [NSThread currentThread]; NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; while(![myThread isCancelled]) [runLoop runMode:NSDefaultRunLoopMode beforeDate:distantFuture]; Then to kill the thread, do [thread cancel] and then you'll have to jog its runloop, so fire off a dummy message to it as well to make it fall out and hit the while again and exit cleanly.
In addition to what Mike said, you should add an NSPort to ensure the runloop has at least one source, or it may exit immediately:
[runLoop addPort:[NSPort port] forMode:NSDefaultRunLoopMode]; while ( )...Adding a timer as others suggested may work, but adding an NSPort is a fairly standard way to do this (the archives give examples of this).
-- Adam
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]