|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Destroying A Code Component ObjectThe 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. Its destroyed automatically when it goes out of scope. If you dont 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 ComponentsYou can show forms from code component classes just as you would in a standard Visual Basic projectyou use the Show method: FormName.Show [ style [, ownerform]] Here are the arguments for Show:
Forms that you show from code components can support controls in the normal Visual Basic way. Lets 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, well 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 were displaying forms from classes in code components. The final code for this example is located in the ExampleClass folder on this books accompanying CD-ROM.
Creating Dialog Box Libraries In Code ComponentsYou 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 applications 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 objects NonModalAllowed property. Lets see an example. Heres 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 ComponentsAt 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. Well 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.
Designing Multithreaded Out-Of-Process ComponentsAt 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, well 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.
|
|
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. |