Of course, a thorough programmer would want to be sure of getting coffee notifications regardless of which application she was using. You could create separate CoffeeMonitor objects for each program in which you wanted to be notified, but that could cause problems if your computer doesn’t have multiple serial ports.
One way to allow multiple clients to hook up to a single CoffeeMonitor object is to provide a connector object, as shown in the following procedure.
To create the Connector object
Option Explicit
Public gCoffeeMonitor As CoffeeMonitor
Public glngUseCount As Long
The variable gCoffeeMonitor
is used to keep a reference to the single shared CoffeeMonitor. The glngUseCount
variable keeps track of the number of Connector objects using the CoffeeMonitor.
Public Property Get CoffeeMonitor() As CoffeeMonitor
Set CoffeeMonitor = gCoffeeMonitor
End Property
The Connector’s read-only CoffeeMonitor property returns a reference to the single global instance of CoffeeMonitor.
Private Sub Class_Initialize()
If gCoffeeMonitor Is Nothing Then
Set gCoffeeMonitor = New CoffeeMonitor
End If
glngUseCount = glngUseCount + 1
End Sub
The first time a client requests a Connector object, it will create the global CoffeeMonitor. Each Connector object increases the use count of the CoffeeMonitor.
Tip The Initialize event of the first object a component creates is a good place to initialize the component. You’ll be much less likely to encounter object creation time-out problems and deadlocks that may occur if Sub Main is used to initialize the component.
Private Sub Class_Terminate()
glngUseCount = glngUseCount - 1
If glngUseCount = 0 Then
Set gCoffeeMonitor = Nothing
End If
End Sub
Just as objects should dispose of any forms they create, so they should release any objects they use. Because the reference to the global CoffeeMonitor is in a global variable, the last Connector object must release it.
Note A compiled out-of-process component shuts down when all clients release their references to its objects, unless it has a loaded form. When compiled, the Coffee component would be kept running by the TestForm that CoffeeMonitor keeps a reference to. Since the CoffeeMonitor object is kept from terminating by the global variable, the component would never shut down. This is discussed in "Starting and Ending a Component," in "General Principles of Component Design."
Important Make sure you have the CoffeeMonitor class active before you set the Instancing property. If you set the Instancing property of the wrong class, you’ll get an error later when you run CoffeeWatch.
Any client can create an instance of the Connector class, because its Instancing property is set to MultiUse (the default). Clients can use a Connector object’s CoffeeMonitor property to get a reference to the single shared CoffeeMonitor object. By making CoffeeMonitor a PublicNotCreatable class, you allow clients to use the shared global instance — while preventing them from creating their own CoffeeMonitors.
When prompted to save the new files, use the following names. Visual Basic supplies the extensions.
File | File name | Extension |
Class module | Coffee_Connector | .cls |
Module | Coffee_Module1 | .bas |
This topic is part of a series that walks you through creating a sample ActiveX EXE.
To | See |
Go to the next step | Using the Shared CoffeeMonitor |
Start from the beginning | Creating an ActiveX EXE Component |