打印本文 打印本文 关闭窗口 关闭窗口
A Thread to Visual Basic
作者:武汉SEO闵涛  文章来源:敏韬网  点击数5503  更新时间:2009/4/23 16:39:28  文章录入:mintao  责任编辑:mintao
n a single application but work completely independently. Internet browsers are a good example of multithreaded clients, where each browser window runs in its own thread. Note that multithreading should not be used as a substitute for good event driven design.

Multithreading DLL

A multithreading DLL does not actually create its own threads. It is simply a DLL that creates objects that run in the same thread that requests the objects. For example: a multithreaded ActiveX control (which is a DLL) creates controls that run in the same thread as the form that contains the control. This can improve efficiency on a multithreaded client such as an Internet browser.

Multithreaded Servers DLL or EXE

In a client server architecture, multithreading can improve performance if you have a mix of long and short client requests. Be careful though -- if all of your client requests are of similar length, multithreading can actually slow down the server''''s average response time! Never assume that the fact that your server is multithreading will necessarily improve performance.

The Threading Contract

Believe it or not, all of this has been in the way of introduction. Some of the material reviews information that is covered in far greater depth in my Developing ActiveX Components book, other material describes new information for service pack 2.

Now, allow me to ask a question that goes directly to the heart of multithreading using COM (the component object model on which all Visual Basic objects, and those in other windows applications that use OLE are based).

Given:

That multithreading is potentially extremely dangerous and specifically -- that attempting to multithread code that is not designed to support multithreading is likely to cause fatal errors and system crashes.

Question:

How is it possible that Visual Basic allows you to create objects and use them under both single and multithreaded environments without any regard to whether they are designed for single or multithreaded use?

In other words -- How can a multithreaded Visual Basic application use objects that are not designed to be thread safe? How can other multithreaded applications use single threaded Visual Basic objects?

In short: how does COM handle threading issues?

If you know about COM, you know that it defines the structure of a contract. A COM object agrees to follow certain rules so that it can be used successfully from any application or object that supports COM.

Most people think first of the interface part of the contract -- the methods and properties that an object exposes.

But you may not be aware of the fact that COM also defines threading as part of the contract. And like any part of the COM contract -- if you break it, you are in very deep trouble.

Visual Basic, naturally, hides most of this from you, but in order to understand what follows, you must learn a little bit about the COM threading models.

The Single Threading Model:

A single threaded server is the simplest type of server to implement. It is also the easiest to understand. In the case of an EXE server, the server runs in a single thread. All objects are created in that thread. All method calls to each object supported by the server must arrive in that thread.

But what if the client is running in a different thread? In that case, a proxy object must be created for the server object. This proxy object runs in the client''''s thread and reflects the methods and properties of the actual object. When a method is called on the proxy object, it performs whatever operations are necessary to switch to the object''''s thread, then calls the methods on the actual object using the parameters passed to the proxy. Naturally, this is a rather time consuming task -- but it does allow the contract to be followed. This process of switching threads and transferring data from the proxy object, to the actual object and back is called marshalling. It is covered in more depth in chapter 6 in my Developing ActiveX Components book.

In the case of DLL servers, the single threading model demands that all objects in the server be created and called in the same thread as the first object created by the server.

The Apartment Threading Model

Note that apartment model threading as defined by COM does NOT require that each thread have its own set of global variables. That''''s just how Visual Basic implemented the apartment model. The apartment model states that each object may be created in its own thread, however once an object is created, its methods and properties may only be called by the same thread that created the object. If another thread wants to access methods of the object, it must go through a proxy.

This is a relatively easy model to implement. If you eliminate global variables (as Visual Basic does), the apartment model grants you thread safety automatically -- since each object is effectively running in its own thread, and due to the lack of global variables, the different object threads do not interact with each other.

The Free Threading Model

The free threading model basically says that all bets are off. Any object can be created in any thread. All methods and properties on any object can be called at any time from any thread. The object accepts full responsibility for handling any necessary synchronization.

This is the hardest model to implement successfully, since it demands that the programmer handle all synchronization. In fact, until recently, OLE itself did not support this threading model! However, since marshalling is never required, this is the most efficient threading model.

Which model does your server support?

How does an application, or Windows itself, know which threading model a server is using? This information is included in the registry. When Visual Basic creates and object, it checks the registry to determine in which cases a proxy object and marshalling are required.

It is the client''''s responsibility to adhere strictly to the threading requirements of each object that it creates.

The CreateThread API

Now let''''s take a look at how the CreateThread API can be used with Visual Basic.

Say you have a class that you want to have running in another thread in order to perform some background operation. A generic class of this type might have the following code (from the MTDemo 3 example):

'''' Class clsBackground
'''' MTDemo 3 - Multithreading example
'''' Copyright © 1997 by Desaware Inc. All Rights Reserved
Option Explicit
Event DoneCounting()
Dim l As Long
Public Function DoTheCount(ByVal finalval&) As Boolean
Dim s As 

上一页  [1] [2] [3] [4] [5] [6] [7]  下一页

打印本文 打印本文 关闭窗口 关闭窗口