Export (0) Print
Expand All

Event Handling in Managed Code

Visual Studio .NET 2003

In managed event handling, you set up an event source and event receiver using the event_source and event_receiver attributes, respectively, specifying type=managed. These attributes inject the appropriate code to allow the classes to which they are applied to fire events and handle events in a managed context.

Note   The .NET Framework has a specific delegate model. If you are developing components for the .NET Framework, see Events and Delegates; this explains the .NET Framework's delegate model for events and shows how you can implement event functionality in your components.

This topic shows a code sample for firing an event in a managed class. For additional code samples using events, see:

Declaring Events

In an event source class, use the __event keyword on a method declaration (or a pointer-to-delegate data member) to declare the method as an event. Make sure to declare the method, but do not define it; to do so will generate a compiler error, because the compiler defines the method implicitly when it is made into an event. Managed events can be data members or methods. Managed event methods can have zero or more parameters.

When used with an event, the return type of a delegate must be complaint with the Common Language Specification. The return type of the event handler must match the return type of the delegate.

Defining Event Handlers

In an event receiver class, you define event handlers, which are methods with signatures (return types, calling conventions, and arguments) that match the event that they will handle.

Hooking Event Handlers to Events

Also in an event receiver class, you use the intrinsic function __hook to associate events with event handlers and __unhook to dissociate events from event handlers. You can hook several events to an event handler, or several event handlers to an event.

Firing Events

To fire an event, simply call the data member or method declared as an event in the event source class. If handlers have been hooked to the event, the handlers will be called.

An event can only be raised from the class where it is declared. This means you need to declare a member function that is accessible from outside the class to raise the event.

Managed Event Code

The following simple example shows how to fire an event in a managed class. To compile and run the example, refer to the comments in the code.

// evh_cpsource.cpp
// compile with: /clr /LD
#using <mscorlib.dll>
using namespace System;

[event_source(managed)]
public __gc class CPSource {
public:
   __event void MyEvent(Int16 nValue);
   void Fire_MyEvent(Int16 nValue) {
      MyEvent(nValue);
   }
};
// evh_cpexecute.cpp
// compile with: /clr
#using <mscorlib.dll>
using namespace System;
#using "evh_cpsource.dll"

[event_receiver(managed)]
__gc class CReceiver {
public:
   void MyHandler1(Int16 nValue) {
      Console::Write("MyHandler1 was called with value ");
      Console::WriteLine(nValue);
   }

   void MyHandler2(Int16 nValue) {
      Console::Write("MyHandler2 was called with value ");
      Console::WriteLine(nValue);
   }

   void HookEvent(CPSource* pSource) {
      __hook(&CPSource::MyEvent, pSource, &CReceiver::MyHandler1);
      __hook(&CPSource::MyEvent, pSource, &CReceiver::MyHandler2);
   }

   void UnhookEvent(CPSource* pSource) {
      __unhook(&CPSource::MyEvent, pSource, &CReceiver::MyHandler1);
      __unhook(&CPSource::MyEvent, pSource, &CReceiver::MyHandler2);
   }
};

int main() {
   CPSource* pSource = new CPSource;
   CReceiver* pReceiver = new CReceiver;
   pReceiver->HookEvent(pSource);
   pSource->Fire_MyEvent(123);
   pReceiver->UnhookEvent(pSource);
}

Program Output

MyHandler1 was called with value 123.
MyHandler2 was called with value 123.

See Also

Event Handling in Visual C++ | Event Handling Keywords | Introduction to the Unified Event Model

Show:
© 2014 Microsoft