Adding a timer to a thread in Cocoa
- 23rd October , 2008 by Sam in Cocoa, Objective-C, iPhone
- 4 comments
Just storing this code away for future reference. It creates a new thread and adds a timer to it’s run loop.
This code creates a new thread and calls the secondThread method on self.
You’ll need to setup a new autorelease pool and start a run loop for your new thread. Ths code below does the following.
- creates a new pool
- creates a new timer to call the secondThreadMethod method every 5 seconds
- adds the timer to the new thread’s run loop
- starts the new thread’s run loop


March 5th, 2009 at 10:57 am
This is handy, thanks.
August 14th, 2009 at 7:48 am
helps a lot! Thanks!
February 25th, 2011 at 9:03 pm
[…] (sample code from the iPhone Developer’s Cookbook) http://samuelschroeder.com/2008/10/23/adding-a-timer-to-a-thread-in-cocoa/ (adding a timer to new thread and starting it) […]
February 27th, 2011 at 8:00 pm
[…] (sample code from the iPhone Developer’s Cookbook) http://samuelschroeder.com/2008/10/23/adding-a-timer-to-a-thread-in-cocoa/ (adding a timer to new thread and starting it) […]