Starting and Stopping a Timer

Normally you install your program’s timers when you’re initializing your program. In this case you want to start the timers yourself instead of having the timers start firing immediately. To start playing a timer, call the function SetEventLoopTimerNextFireTime().This function takes two arguments. The first argument is the timer. The second argument is the amount of time to wait to fire the timer. Pass the value kEventDurationNoWait as the second argument, which tells the operating system to start the timer.

OSStatus error; 
error = SetEventLoopTimerNextFireTime(timer, kEventDurationNoWait);

Sometimes you need to temporarily turn off timers. If you use a timer to run your game’s event loop, you want to turn off the timer when the player pauses the game. To pause the timer call SetEventLoopTimerNextFireTime(), but supply the value kEventDurationForever as the second argument. The following call pauses the timer:

OSStatus error; 
error = SetEventLoopTimerNextFireTime(timer, kEventDurationForever);

Removing a Timer

When the user quits your program, you must remove the timer you installed as well as the UPP you created for the timer. Call the function RemoveEventLoopTimer() to remove the timer, supplying the timer name. Call the function DisposeEventLoopTimerUPP() to remove the UPP.

OSStatus error;
error = RemoveEventLoopTimer(timer); 
    
DisposeEventLoopTimerUPP(timerUPP);

Conclusion

Unlike my previous articles, there is no source code to accompany this article. If you want to see examples of event timers, download the source code for my QuickTime and AGL articles. Both programs provide examples of using event timers in an application.

Previous (Writing a Timer)