This chapter has already discussed how to use events to provide a callback
mechanism between a class object and its clients (see the section titled
"Raising the Event and Implementing Callbacks in a Class Object").
This section and the following sections discuss a somewhat more complicated
way to allow an ActiveX client to receive asynchronous callback notifications
from objects. This second method requires the client and the server object to
share a callback object. A callback object is an object that client and server
share so that they can communicate with each other.
Because client and server share the callback object, they must agree on the
structure of the object (that is, its properties and methods). A callback object
is therefore a perfect candidate for an Interface as discussed in the previous
sections. Both client and server programs (and therefore programmers!) must cooperate
for the callback object to work. The server will define the callback object's
Interface, but the client will actually implement the Interface in another class
of its own, instantiate an object from that implementation, and pass the instantiated
object to the server. The server can then manipulate the callback object to provide
notifications to the client.
A client-server callback object's lifetime goes through the following steps:
-
The server provides an abstract Interface that gives an abstract definition
of the callback object (that is, its methods and properties). The server, however,
does not typically implement the callback object. The Interface defines at least
one callback method.
-
The client implements the callback object from the abstract definition given
by the Interface. The client defines how the callback method that it derived from
the Interface will function.
-
The client instantiates the callback object from its implementation of the
Interface.
-
The client calls a Server method, passing the callback object as an argument.
-
The server receives the callback object and sets a classwide variable to point
to the object. This makes the callback object available throughout the Server
class's code.
-
When the server needs to notify the client, it invokes the callback method
of the callback object.
-
The callback method then behaves in whatever manner that the client has specified
in its implementation—typically providing some sort of notification that
the client can use.
To make these seven steps work, the server programmer and the client programmer
must perform three major tasks:
-
The server programmer provides the Interface that defines the callback object
class's properties and methods. One method is typically considered the callback
method.
-
The client programmer implements the callback class from its Interface definition
and then passes an instance of the resulting object to the Server object by calling
the appropriate method or methods of the server.
-
The server programmer provides a method that receives an instance of the callback
object and then assigns a classwide object variable to point to this instance.
The server will call the object variable's callback method whenever it needs to
notify the client.
The following sections discuss each of these tasks.
-
Providing an Interface for the Callback Object
-
Implementing the Callback Object in the Client
-
Manipulating the Callback Object in the Server