Carbon (API) - Event Handling

Event Handling

The Mac Toolbox's Event Manager originally used a polling model for application design. The application's main event loop asks the Event Manager for an event using GetNextEvent. If there is an event in the queue, the Event Manager passes it back to the application, where it is handled, otherwise it returns immediately. This behavior is called "busy-waiting", running the event loop unnecessarily. Busy-waiting reduces the amount of CPU time available for other applications and decreases battery power on laptops. The classic Event Manager dates from the original Mac OS in 1984, when whatever application was running was guaranteed to be the only application running, and where power management was not a concern.

With the advent of MultiFinder and the ability to run more than one application simultaneously came a new Event Manager call, WaitNextEvent, which allows an application to specify a sleep interval. One easy trick for legacy code to adopt a more efficient model without major changes to its source code is simply to set the sleep parameter passed to WaitNextEvent to a very large value—on OS X, this puts the thread to sleep whenever there is nothing to do, and only returns an event when there is one to process. In this way, the polling model is quickly inverted to become equivalent to the callback model, with the application performing its own event dispatching in the original manner. There are loopholes, though. For one, the legacy toolbox call ModalDialog, for example, calls the older GetNextEvent function internally, resulting in polling in a tight loop without blocking.

Carbon introduces a replacement system, called the Carbon Event Manager. (The original Event Manager still exists for compatibility with legacy applications). Carbon Event Manager provides the event loop for the developer (based on Core Foundation's CFRunLoop in the current implementation); the developer sets up event handlers and enters the event loop in the main function, and waits for Carbon Event Manager to dispatch events to the application.

Read more about this topic:  Carbon (API)

Famous quotes containing the words event and/or handling:

    A society which allows an abominable event to burgeon from its dungheap and grow on its surface is like a man who lets a fly crawl unheeded across his face or saliva dribble unstemmed from his mouth—either epileptic or dead.
    Jean Baudrillard (b. 1929)

    For a novel addressed by a man to men and women of full age; which attempts to deal unaffectedly with the fret and fever, derision and disaster, that may press in the wake of the strongest passion known to humanity; to tell, without a mincing of words, of a deadly war waged between flesh and spirit; and to point the tragedy of unfulfilled aims, I am not aware that there is anything in the handling to which exception can be taken.
    Thomas Hardy (1840–1928)