Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Destroying A Code Component Object

The Testing Department is on the phone again. The objects created with your new code component are great, but they seem to use up a lot of memory. How can you free that memory?

There are two ways to destroy an object. It’s destroyed automatically when it goes out of scope. If you don’t want to wait for that to happen, you can just set its variable using the Visual Basic Nothing keyword, which destroys the object and releases its memory:

Set objBigObject = Nothing

Using Forms From Code Components

You can show forms from code component classes just as you would in a standard Visual Basic project—you use the Show method:

FormName.Show [ style [, ownerform]]

Here are the arguments for Show:

  style—Integer that determines if the form is modal (vbModal = 1) or modeless (vbModeless = 0). (For more on using modeless forms in in-process code components, see the next topic.)
  ownerform—String that specifies the component that owns the form you want to show (for instance, you can use the keyword Me here).

Forms that you show from code components can support controls in the normal Visual Basic way. Let’s see an example. When we created the NewClass code component in the beginning of this chapter, we left the default form, Form1, in the project. To show that form, we just add a new method, ShowForm, to the ExampleClass class in NewClass, and we show Form1 in that method:

Public Sub ShowForm()
    Form1.Show
End Sub

In addition, we’ll add a label with the caption “Code Component Form” in Form1. Now in the client application, we can make use of this new method to show Form1 this way:

Private Sub Command5_Click()
    objExample.ShowForm
End Sub

The result appears in Figure 27.6. Now we’re displaying forms from classes in code components. The final code for this example is located in the ExampleClass folder on this book’s accompanying CD-ROM.


Figure 27.6  Displaying a form from a code component.

Creating Dialog Box Libraries In Code Components

You can use code components to provide libraries of modal and modeless dialog boxes (for more on creating dialog boxes, see Chapter 4). There is one consideration, however, that we should address. It turns out that modeless dialog boxes (that is, dialog boxes that can be open at the same time the user is working in another form) displayed by in-process code components (ActiveX DLLs) cannot work unless they can communicate with the client application’s internal message loop. This means that in-process components can only display modeless dialog boxes in client processes that support such communication. You can test if the client application supports this communication by checking the Visual Basic App object’s NonModalAllowed property.

Let’s see an example. Here’s some code that might appear in an in-process code component that displays a dialog box. If the application supports access to its message loop, we can show the dialog box, Form1, as modeless by passing the constant vbModeless to the Show method:

Public Sub ShowForm()
    If App.NonModalAllowed Then
        Form1.Show vbModeless
...

Otherwise, you should display the dialog box as modal:

Public Sub ShowForm()
    If App.NonModalAllowed Then
        Form1.Show vbModeless
    Else
        Form1.Show vbModal
    End If
End Sub

Designing Multithreaded In-Process Components

At the beginning of this chapter, we discussed some thread serialization concerns for code components; in particular, we saw that to avoid blocked threads, you can use multithreaded code components. We’ll see how that works for in-process components in this topic and take a look at out-of-process components in the next topic.

To make an in-process code component multithreaded, select the Project|Properties menu item and click the General tab. For ActiveX DLLs, you can select two options in the Threading Model box: Single Threaded or Apartment Threaded. Selecting Apartment Threaded makes the in-process code component multithreaded.


TIP:  Set the threading model before you add controls to your project. When you change the threading model for an existing project, errors will occur if the project uses single-threaded ActiveX controls. Visual Basic doesn’t allow the use of single-threaded controls in apartment-threaded projects.

Designing Multithreaded Out-Of-Process Components

At the beginning of this chapter, we discussed some thread serialization concerns for code components; in particular, we saw that to avoid blocked threads, you can use multithreaded code components. We saw how that worked for in-process components in the previous topic. In this topic, we’ll take a look at out-of-process components.

To make an out-of-process code component multithreaded, select the Project|Properties menu item and click the General tab. For ActiveX EXEs, you can select two options in the Threading Model box: Thread Per Object or Thread Pool.

To make the project multithreaded, you can specify that each new object is created on a new thread with the Thread Per Object option, or you can limit your component to a fixed pool of threads by setting up a thread pool. A thread pool size of 1 makes the out-of-process code component single-threaded, whereas a larger thread pool makes the project apartment-threaded.


TIP:  Set the threading model before you add controls to your project. When you change the threading model for an existing project, errors will occur if the project uses single-threaded ActiveX controls. Visual Basic doesn’t allow the use of single-threaded controls in apartment-threaded projects.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.