Preventing sleep

Q: How can I prevent system sleep while my application is running?

A: Most applications should not prevent system sleep, however, there are exceptions. For example, if your application is playing a long movie or slideshow, the user will probably get annoyed if the computer keeps going to sleep. Your application can prevent system sleep by calling UpdateSystemActivity once every 30 seconds. The following code shows how to create a CFRunLoopTimer that calls UpdateSystemActivity every 30 seconds.

Listing 1: Example usage of UpdateSystemActivity.

#include <CoreServices/CoreServices.h>

MyTimerCallback(CFRunLoopTimerRef timer, void *info)

main (int argc, const char * argv[])
    CFRunLoopTimerRef timer;
    CFRunLoopTimerContext context = { 0, NULL, NULL, NULL, NULL };

    timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent(), 30, 0, 0, MyTimerCallback, &context);
    if (timer != NULL); {
        CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);

    /* Start the run loop to receive timer callbacks. You don't need to
    call this if you already have a Carbon or Cocoa EventLoop running. */


    return (0);

Document Revision History

Date Notes
2004-10-22 Explains how your application can prevent the system from sleeping.

Posted: 2004-10-22

Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.