Carbon Event Timers

Author: Mark Szymczyk
Last Update: October 4, 2005

This article provides an introduction to Carbon event timers. After reading the article you will know how to write an event timer, install it, start it, and stop it.

Introduction

A timer is a function the operating system calls periodically. You get to specify how often the timer fires, which is how often the operating system calls the timer. Use a timer when you want to do something repeatedly in your program at regular time intervals. Examples of using timers in Mac OS X programs include the following:

  • Maintaining a constant frame rate in games. Create an event timer that calls the game’s main game loop and fires at the desired frame rate.
  • Updating a clock display.
  • Periodically opening a dialog box in shareware programs asking the user to register the program.
  • Blinking a text insertion caret.

To use timers you need two variables in your program: one variable of type EventLoopTimerRef, and one variable of type EventLoopTimerUPP.

EventLoopTimerRef timer;
EventLoopTimerUPP timerUPP;

There are three tasks you must perform to use timers in your applications: install the timer, write the timer, and start the timer. I cover these tasks in the next three sections.

Next (Installing a Timer)