Partilhar via


Visual Basic Concepts

Using the Shared CoffeeMonitor

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

  1. Switch to the instance of Visual Basic that has the CoffeeWatch project loaded.

  2. In the Project Explorer window of the CoffeeWatch project, right-click Form1 to open the context menu, and select View Code to open the code window. Modify the code in the Declarations section as follows:

    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.

  3. Change the code in the Load event of Form1, so that it first creates a Connector object, and then obtains a reference to the shared CoffeeMonitor object using the Connector’s CoffeeMonitor property.

    Private Sub Form_Load()
       Set mcctTest = New Connector
       Set mwcmnTest = mcctTest.CoffeeMonitor
    End Sub
    
  4. Press F5 to run the project.

    Once again, you’ll get a notification from Coffee every ten seconds.

  5. Close the CoffeeWatch form to return to design mode.

  6. Switch to the Coffee project, and click the Break button (or select Break from the Run menu) to enter break mode. In the Immediate window, type:

    ?TypeName(gCoffeeWatch)
    

    The result is Nothing, showing that the shared CoffeeMonitor object was released.

  7. Click the End button to return to design mode.

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.

A Bug in Connector

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.

Asynchronous Call-Back Methods

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.

Multithreading

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

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 Creating and Testing the Coffee Executable
Start from the beginning Creating an ActiveX EXE Component