Partager via


Visual Basic Concepts

Sharing the CoffeeMonitor

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

  1. Switch to the instance of Visual Basic containing the Coffee project, and click the End button to return to design mode.

  2. On the Project menu, select Add Module to add a standard module to the Coffee project. (If you have the Add Module dialog box enabled, double-click the Module icon when the dialog box appears.) Add the following variable declarations to the Declarations section:

    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.

  3. On the Project menu, select Add Class Module to add a class module to the Coffee project. (If you have the Add Class Module dialog box enabled, double-click the Class Module icon when the dialog box appears.) In the Properties window, set (Name) to Connector.

  4. Add the following code to the Connector class’s code window:

    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.

  5. In the Object drop down, select Class to open the template for the default event (Initialize). Add the following code to create the 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.

  1. In the Procedure drop down, select the Terminate event for the class. Add the following code to the event procedure template:

    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."

  2. In the Project Explorer window, double-click CoffeeMonitor to activate it. In the Properties window, set the Instancing property of the class to PublicNotCreatable.

    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.

  3. Press CTRL+F5 to run the project. Remember, when working with out-of-process components, the component project must be in run mode before you can edit or run the client project.

    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

Step by Step

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