Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Processing Events Manually

In most cases, using the RunApplicationEventLoop function to collect and dispatch events is the simplest and most practical way to handle events. However, sometimes you may want more control over the event collection and dispatching mechanism, or you may need to process events that don’t occur in the main application thread. In cases like these, you can call other Carbon Event Manager functions to manually collect and dispatch your events.

The RunApplicationEventLoop function itself calls several Carbon Event Manager functions to accomplish its task:

Listing 2-10 shows how you can use these calls to implement the basic functionality of RunApplicationEventLoop.

Listing 2-10  Processing events manually

EventRef theEvent;
EventTargetRef theTarget;
 
theTarget = GetEventDispatcherTarget();
 
    while  (ReceiveNextEvent(0, NULL,kEventDurationForever,true,
            &theEvent)== noErr)
        {
 
            SendEventToEventTarget (theEvent, theTarget);
            ReleaseEvent(theEvent);
        }

The ReceiveNextEvent function is blocked forever (kEventDurationForever) until an event occurs. Specifying zero and null for the first two parameters indicates that ReceiveNextEvent should return on all events. (Alternatively, you could specify that the function wait only for particular events). Passing true in the third parameter indicates that the application should take ownership of the event (which means it is pulled off the event queue).

After an event occurs, we dispatch it to the event dispatcher target, which automatically sends it to the proper event target. Because the application owns the event, the application is then responsible for releasing it by calling ReleaseEvent. (There is also a complementary function RetainEvent, which you can use to increment the reference count of the event, thus ensuring that it will not get disposed before you are finished with it.)

The only drawback to making your own event loop dispatching calls in the main application thread is that you won’t get the standard application event handler installed. Specifically, the RunApplicationEventLoop function installs handlers to do the following:

One way to work around this limitation is by creating a dummy custom event handler. When you are ready to process events, create the dummy event yourself, post it to the queue. and then call RunApplicationEventLoop (to install the standard application event handler). The dummy event handler can then process the events manually. For an example of using this method, see Technical Q&A 1061 in Developer Documentation Technical Q&As.



< Previous PageNext Page > Hide TOC


Last updated: 2005-07-07




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.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice