Responding to COM Events in .NET Applications
In This Chapter
Callbacks in .NET
Callbacks in COM
Handling COM Events in Managed Code
Handling ActiveX Control Events in Managed Code
In both COM and .NET, events play a central role in bi-directional communication. Bi-directional communication refers to the paradigm in which clients call into components, which in turn call back into clients. In graphical user interfaces, for example, bi-directional communication is natural because clients of user interface objects such as buttons, check boxes, and tabs wish to be notified when the human user changes their states. Besides graphical applications, callback functionality is commonly used for asynchronous processing when a method has a lot of work to do, or used to let clients insert their own logic inside a method's algorithm. For example, the caller of a sorting algorithm could be called back whenever two objects in a collection need to be compared, so the client can customize the algorithm to suit its needs.
The title of this chapter is overly simplified because, strictly speaking, COM doesn't have built-in events like .NET does. Instead, COM components often use a standard connection points protocol, described in this chapter, that tools like Visual Basic 6 are able to conceal in an abstraction of events. The developers of COM Interoperability went to great lengths to make "events" in COM be exposed as .NET events, but some aspects of this support are not intuitive to people encountering it for the first time. This chapter tells you everything you need to know about handling a COM component's events in managed code.