Visual Basic Concepts
Asynchronous Notifications Using Events
There are two parts to implementing asynchronous processing using events. The first part is the responsibility of the author of a component. The author must:
Define the tasks or notifications to be performed.
Provide one or more externally creatable classes to manage the tasks or notifications. This manager class may also do the work, or a worker class may be provided to do the actual processing.
Provide the manager class with methods that clients can call to initiate tasks or to request notifications.
Declare the events that clients must handle in order to receive notifications.
Write code to start the task, or the process of watching for interesting occurrences.
Write code to raise the event when the task is complete, or when the interesting occurrences are observed.
The second part is the responsibility of the developer who uses the component. The developer must:
Create a WithEvents variable to contain a reference to the object that will provide the notification events.
In the event procedures associated with the WithEvents variable, write code to handle the desired notification events.
Write code to request an instance of the component’s manager class, and place the reference to it in the WithEvents variable.
Write code to call the methods that initiate tasks or that request notifications.
Figure 8.12 shows how the author’s part and the developer’s part interact to enable asynchronous processing for the CoffeeReady example from the step-by-step procedures in "Creating an ActiveX EXE Component."
Figure 8.12 Asynchronous notifications using events
Note The numbers in Figure 8.12 indicate the order in which things happen in the finished application and component. They do not correspond to the numbers in the task lists.
A single event can be handled by multiple clients. One way to connect multiple clients to a single Coffee object would be to interpose a Connection object between the client and the Coffee object. In this way, each client would have its own Connection object, and each Connection object would supply its client with a reference to one central Coffee object.
The Coffee object’s CoffeeReady event would be received by all clients that had a WithEvents variable containing a reference to the Coffee object.
Note Visual Basic’s events can be thought of as anonymous. That is, the object that raises the event has no way of knowing whether a given event is handled by one, two, or two dozen objects — or by no objects at all. In addition, the object that raises an event has no way of knowing whether errors occur in the event-handling code of other objects.
Important When you’re designing a system in which multiple clients receive the same event, do not make assumptions about the order in which clients receive the event. The order is undefined, and may differ depending on version number or platform.
For More Information You can see these tasks carried out in the step-by-step procedures in "Creating an ActiveX EXE Component," which demonstrates asynchronous notifications using both events and call-back methods.