This procedure shows how to use the Connector object to get a reference to a single shared CoffeeMonitor object.
Note This topic is part of a series that walks you through creating a sample ActiveX EXE. It begins with the topic Creating an ActiveX EXE Component.
To receive the CoffeeReady event in CoffeeWatch
Option Explicit
Private WithEvents mwcmnTest As CoffeeMonitor
Private mcctTest As Connector
The new variable, mcctTest
, will hold a reference to the Connector object. The letters ‘cct’ have been chosen to indicate a variable of type Connector.
Private Sub Form_Load()
Set mcctTest = New Connector
Set mwcmnTest = mcctTest.CoffeeMonitor
End Sub
Once again, you’ll get a notification from Coffee every ten seconds.
?TypeName(gCoffeeWatch)
The result is Nothing, showing that the shared CoffeeMonitor object was released.
You may find it interesting to run the Coffee project again, then make CoffeeWatch.exe and run multiple instances of it. (You must run the Coffee project first, so that its type library information is available.) You can set break points in Coffee, to observe the workings of the events and properties of its objects while it provides objects to several clients.
There’s a bug in the Connector class. If all clients release their Connector objects (but keep their references to the shared CoffeeMonitor), the last Connector will release the global reference. At that point, any client that creates a Connector will cause a second CoffeeMonitor to be created — because the new Connector will find that the global variable gCoffeeWatch
contains Nothing. If CoffeeMonitor really used the serial port, this could cause a conflict.
The sample applications Coffee2.Vbp and CoffWat2.Vbp explore a possible solution to this bug.
An alternate technique for notifying clients is the asynchronous call-back method, discussed in "Asynchronous Notifications Using Call-Back Methods." Call-backs are somewhat more complicated to implement, but they allow the component to receive return values and errors raised by the client, and they have a slight performance advantage.
The sample applications Coffee2.Vbp and CoffWat2.Vbp include an alternate implementation of CoffeeReady using call-backs.
Code components with no user interface can be marked for unattended execution, which is to say they can have no user interaction. For out-of-process components this option also enables multithreading, as explained in "Scalability and Multithreading," in "Building Code Components."
The sample application MTCoffee.vbp demonstrates a simple multithreading scenario.
For More Information The basics of raising and handling events are introduced in "Adding Events to a Class," in "Programming with Objects" in the Visual Basic Programmer’s Guide. Events in components are discussed in "Adding Events to Classes" in "General Principles of Component Design."
This topic is part of a series that walks you through creating a sample ActiveX EXE.
To | See |
Go to the next step | Creating and Testing the Coffee Executable |
Start from the beginning | Creating an ActiveX EXE Component |