|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Multithreading Code Components You make in-process and out-of-process code components multithreaded by changing their threading model. Visual Basic uses its apartment-model threading to ensure thread safety when youre working with multiple threads. In apartment-model threading, each thread is like an apartmentall objects created on the thread work in an apartment, and they are not aware of the objects in other apartments. This threading model also eliminates conflicts when accessing global data from multiple threads by giving each apartment its own copy of that global data. You can use apartment-model threading without having to eliminate visual elements such as forms and controls, because all such Visual Basic objects are thread-safe. You can specify the apartment threading model (instead of the single-threaded model) for in-process code components to make them multithreaded. To make out-of-process code components multithreaded, you have two options (both use the apartment thread model): thread pooling and the thread-per-object model. With thread pooling, you can specify how many threads you want available for your code components, and those threads are used in a round-robin way (that is, the first object is created on the first thread in the pool and thread allocation keeps going thread by thread, starting over with the first thread when the others in the pool are used). If you specify the thread-per-object model, each new object is created with its own thread. SingleUse Code Components You can also handle serialization conflicts by creating SingleUse code components, in which every new object of your code component class creates a new instance of the component. You do this by setting the classs Instancing property. The Instancing property indicates how you want your class to interact with client applications, if at all. Here are the possible values of that property:
Overall, then, the reason you use code components is to make it easy to reuse your code in other applications, and well see how that works now in the Immediate Solutions section. Immediate SolutionsUsing A Code Component From A Client ApplicationThe Testing Department is on the phone again. Wouldnt it be great to use Microsoft Excel as part of your new program? Maybe, you say. Its easy, they say, just treat Excel as a code component. You can treat OLE automation servers like Excel as code components, adding them to your program and using them as you like. To do that, you add a reference to the code component to your program, create an object from that code component, then use the properties and methods of that object. Lets see an example. Here, well use Microsoft Excel 7 in a Visual Basic program to add 2 and 2 and display the result in a text box. Start by adding a reference to Excel with the Project|References menu item, selecting the item labeled Microsoft Excel Object Library, and clicking on OK. After youve added a reference to a code component, you can use the Object browser to take a look at the properties and methods in that component, as in Figure 27.1, where we are examining the Excel code library.
Add a button, Command1, with the caption Add 2 + 2 using Microsoft Excel to the program now, as well as a text box, Text1, where we can display the results of the addition. When the user clicks the button, we start by creating an object, objExcel, from the Excel library (Excel will stay hidden throughout the program) using the Visual Basic CreateObject function (for other ways to create an object, see the next topic):
Private Sub Command1_Click()
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Sheet")
...
Now that we have our new object, we can use its properties like this, where we place 2 in both cells (1,1) and (2,1):
Private Sub Command1_Click()
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Sheet")
objExcel.Cells(1, 1).Value = "2"
objExcel.Cells(2, 1).Value = "2"
...
To add these values together, we place an Excel formula in the cell beneath these two, cell (3,1) to add those values:
Private Sub Command1_Click()
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Sheet")
objExcel.Cells(1, 1).Value = "2"
objExcel.Cells(2, 1).Value = "2"
objExcel.Cells(3, 1).Formula = "=R1C1 + R2C1"
...
Finally, we display the resulting sum in the text box in our program, Text1, and quit Excel like this:
Private Sub Command1_Click()
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Sheet")
objExcel.Cells(1, 1).Value = "2"
objExcel.Cells(2, 1).Value = "2"
objExcel.Cells(3, 1).Formula = "=R1C1 + R2C1"
Text1.Text = "Microsoft Excel says: 2 + 2 = " & objExcel.Cells(3, 1)
objExcel.Application.Quit
End Sub
|
|
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. |