Export (0) Print
Expand All

Introduction to the Unified Event Model

Visual Studio .NET 2003

In the past, various programming environments have provided their own separate ways for components to pass back information about asynchronous "events" to their clients. The C language has function callbacks that work well for this, but C++ has always lacked this kind of callback for an object's methods. You can spend considerable time inventing ways to work around this gap in C++ (using window messages, event interfaces, hard-coded callback method names, thunks, and so on). COM defines an event model, but it is tedious to implement and maintain, and does not provide event support between two native C++ objects.

The purpose of the Unified Event Model is to allow applications to use events in a way that minimizes the knowledge and dependencies that a component has on its clients and maximizes the component's reusability, and to do this in a consistent way for native (non-COM) C++, COM, and managed classes.

The model supports single- and multithreaded usage and protects data from simultaneous multithread access. It also allows you to derive subclasses from event source or receiver classes and support extended event sourcing/receiving in the derived class.

Event Handling Elements

An event source is an object that defines and contains events. You create an event source using the event_source attribute.

An event is a method within an event source that, when called, generates events. You define an event using the keyword __event. To raise an event means to "fire" it, by calling the event method.

A delegate is a class that can hold a reference to a method. A delegate class differs from other classes in that it has a signature and can hold references only to methods that match its signature. The .NET Framework has a specific delegate model; if you are developing components for the .NET Framework, see Events and Delegates.

An event receiver (also called an event sink) is an object that receives events. You create an event receiver using the event_receiver attribute.

An event handler is a method in an event receiver that receives events.

To hook an event means to associate (register) an event with an event handler. You associate a handler method with an event using the intrinsic function __hook.

To unhook an event means to dissociate (deregister) an event from an event handler. You dissociate a handler method from an event using the intrinsic function __unhook.

Attributes and Keywords Supporting Events

Event Source

Use event_source and __event on the event source class, as follows:

[event_source(native)]   // optional for native C++ and managed classes
class CSource {
public:
   __event void MyEvent(int nValue);
};

Event Receiver

You typically use event_receiver, __hook, and __unhook on the event receiver class, as follows:

[event_receiver(native)]   // optional for native C++ and managed classes
class CReceiver {
public:
   void MyHandler1(int nValue) {
      printf("MyHandler1 was called with value %d.\n", nValue);
   }
   void hookEvent(CSource* pSource) {
      __hook(&CSource::MyEvent, pSource, &CReceiver::MyHandler1);
   }
   void unhookEvent(CSource* pSource) {
      __unhook(&CSource::MyEvent, pSource, &CReceiver::MyHandler1);
   }
};

Note that the Unified Event Model supports dynamic hooking, that is, hooking/unhooking events in a loop or using conditional logic in a function that receives the source and local method as parameters.

See Also

Event Handling in Visual C++ | Event Handling Keywords

Show:
© 2014 Microsoft