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:
Note You can create type libraries with Visual Basic, as explained in "Creating Standard Interfaces with Visual Basic," in "General Principles of Component Design."
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.
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:
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.
Tip All the methods of an interface must be implemented, but those you don’t use can simply contain a comment.
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)
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.