Visual Basic Concepts
Asynchronous Notifications Using Call-Back Methods
There are two parts to implementing asynchronous processing using call-back methods. 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.
Create a type library containing the interface (or interfaces) that clients must implement in order to receive notifications. This interface must include all methods the component will call to notify the client.
Note You can create type libraries with Visual Basic, as explained in "Creating Standard Interfaces with Visual Basic," in "General Principles of Component Design."
Provide the manager class with methods that clients can call to initiate tasks or to request notifications.
Note Each of these methods must have one argument declared as the interface type defined in step 3, so the client can pass the interface containing its implementation of the appropriate call-back method.
Write code to start the task, or the process of watching for interesting occurrences.
Write code to invoke the appropriate call-back method when the task is complete, or when the interesting occurrences are observed.
Tip If a client provides a number of asynchronous services, you may want to group closely related call-backs on separate interfaces. This is because a client class must implement all the methods on an interface, whether it uses them or not. Having one big interface with all your call-back methods on it is thus inconvenient for clients.
The second part is the responsibility of the developer who uses the component. The developer must:
Create a public class that implements the interface defined by the component author.
Note Use PublicNotCreatable for the Instancing property of this class. The class must be public so the component can invoke the call-back methods, but there’s no reason to let other applications create instances of the class.
In the call-back methods the client will use, write code to handle the notifications.
Tip All the methods of an interface must be implemented, but those you don’t use can simply contain a comment.
Write code to request an instance of the component’s manager class.
Write code to call the methods that initiate tasks or that request notifications.
Figure 8.13 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.13 Asynchronous notifications using call-back methods
Note The numbers in Figure 8.13 indicate the order in which things happen in the finished application and component. They do not correspond to the numbers in the task lists.
Tip If the NotifyMe object is only going to be used once, the client doesn’t have to keep a reference to it. In this case, the client code to call TellMeReady
could be written as follows:
Call mcof.TellMeReady(New NotifyMe)
Call-Backs to Multiple Clients
One way for the Coffee object to handle multiple clients would be for the component to interpose a Connection object between the client and the Coffee object — so that 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 TellMeReady method would have to place the ICoffeeNotify references into a collection, so the timer could enumerate the collection and call each client’s CoffeeReady method.
For More Information You can see these tasks carried out in the sample applications for "Creating an ActiveX EXE Component," which demonstrate asynchronous notifications using both events and call-back methods. Implementing interfaces is discussed in "Providing Polymorphism by Implementing Interfaces," in "General Principles of Component Design." Polymorphism is introduced in "Polymorphism," in "Programming with Objects," in the Visual Basic Programmer’s Guide.