Visual Basic Concepts

Visual Studio 6.0

Scalability and Multithreading

When a component has one thread of execution, code for only one object can execute at any given time. The Automation feature of the Component Object Model (COM) deals with this situation by serializing requests. That is, the requests are queued and processed one at a time until all have been completed.

In a multithreading operating environment, serialization protects single-threaded objects from overlapping client requests — that is, from code in a property or method being executed while one or more previous client requests are still being executed. Overlapping requests can cause internal data errors if objects aren't designed for reentrancy, as discussed in "Apartment-Model Threading in Visual Basic."

Serialization is thus an extremely important feature of Automation. However, serialization of single-threaded components means that requests are sometimes blocked. For example, suppose you're using a Widget object that has two methods:

  • The Spin method takes anywhere from several seconds to half an hour.

  • The Flip method is almost instantaneous.

Because 32-bit applications are preemptively multitasked, a second application could call the Flip method while the Spin method is already running. As shown in Figure 8.2, the short Flip method is blocked until the long Spin method is complete.

Figure 8.2   Blocking in a component with MultiUse objects

When short operations are blocked by long ones, productivity suffers and user frustration rises. Components that behave in this fashion are said to scale poorly. That is, they work poorly if many requests of mixed length are made.

Visual Basic has two component features for avoiding blocked calls — multithreading and SingleUse objects. These features are described in the following topics:

Show: