Mostrar / Ocultar Avisos

Adding a timer to a thread in Cocoa

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

4 Responses

  1. Ari Braginsky Says:

    This is handy, thanks.

  2. springrider Says:

    helps a lot! Thanks!

  3. Mobile App Developers List « UK iPhone App Developers Says:

    […] (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) […]

  4. iPhone Application Development Resources « ProductiseIT Says:

    […] (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) […]

Leave a Reply