Partager via


Visual Basic Concepts

Running the TestThing Test Application

The procedures in this topic illustrate the key events in the lifetime of an object provided by an in-process component, including what happens when the DLL is unloaded forcefully by clicking the End button.

Note   This topic is part of a series that walks you through creating a sample ActiveX DLL. It begins with the topic Creating an ActiveX DLL.

To run the TestThing test application

  1. Press CTRL+F5 to run the project group. Notice that no messages have appeared in the Immediate window yet.

    Note   By default the Compile On Demand option is checked on the General tab of the Options dialog box (accessed from the Tools menu). When you’re debugging a component with Compile On Demand checked, you may find it useful to use CTRL+F5 (or Start with Full Compile, on the Run menu) to compile all the projects in the group before entering run mode. Compile errors usually require resetting the project, which means returning to design mode.

  2. On the Thing Demo dialog box, click Create New Thing. Before you respond to the InputBox requesting a name for the Thing, notice that the Immediate window now contains two debug messages:

    When the Command1_Click event procedure creates the test Thing object, two things happen. First, before the object is created, the code in Sub Main is executed. Only afterSub Main executes is the Thing object created.

    Notice that the Name property has no value yet.

    Important   The Sub Main procedure of an ActiveX component is executed when the component receives the first request for one of the objects it provides, before the component creates the object. You should not put lengthy tasks in the Sub Main procedure, because a request to create an object may time out while waiting for Sub Main to execute. This is discussed in "Starting and Ending a Component" in "General Principles of Component Design."

  3. Type First Thing in the InputBox and click OK. The code you put in the Command1_Click event procedure assigns the value to the Name property of the Thing object.

  4. Click Show the Thing to display the Thing object’s properties. Notice that the Name property now has a value. Click OK to dismiss the message box.

  5. Click Reverse the Thing’s Name to call the ReverseName method of the Thing object. After the method returns, Show the Thing (Command2) is clicked by setting its Value property to True, and the MsgBox statement in Command2_Click() displays the updated property values. Click OK to dismiss the message box.

  6. Click Create New Thing again, to destroy the existing Thing object and create a new one.

    In the Immediate window, you will see a debug message from the Initialize event of the new Thing, as the New operator causes the object to be created. This is followed by the Terminate message from the original Thing, the one you named First Thing.

    The original Thing object is destroyed when the reference to the new Thing object is placed in the variable mthTest. At that point, there are no variables holding references to the original Thing, so it must be destroyed.

  7. Type Second Thing in the InputBox, then click OK.

  8. Click the Close box on Thing Demo to return to design mode. Do not use the End button on the Visual Basic toolbar.

    Before the program closes, the Terminate message for the Thing object you named Second Thing is displayed in the Immediate window.

    When you close a program, Visual Basic cleans up all the variables that still contain object references. As each variable is set to Nothing, the object it referred to is destroyed.

When a program is ended abruptly, with the End button or by an End statement in code, Visual Basic reclaims any memory and resources the program was using. However, the cleanup is done as if the program had suffered a fatal error. Objects will not get their Terminate events.

To observe the effects of the End button

  1. Run the program again. Create a new Thing, and name it anything you like.

  2. This time, end the program using the End button on the toolbar. The Terminate message for the form’s Thing object is not displayed in the Immediate window.

    Important   Remember that ending your program with the End button, or with an End statement in your code, halts the program immediately, without executing the Terminate events of any objects. It’s always better to shut your program down by unloading all the forms.

For More Information*   The rules of object lifetime are introduced in "Programming with Objects" in the *Visual Basic Programmer’s Guide.

Step by Step

This topic is part of a series that walks you through creating a sample ActiveX DLL.

To See
Go to the next step Circular References and Object Lifetime
Start from the beginning Creating an ActiveX DLL.