18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

THE MICROSOFT COMPONENT OBJECT MODEL<br />

Aggregation is important in this case since there are some hidden interfaces defined<br />

on a feature that cannot be contained.<br />

Visual Basic 6 does not support aggregation, so it can’t be used to create custom<br />

features.<br />

Although an understanding of apartments and<br />

threading is not essential in the use of<br />

ArcObjects, basic knowledge will help you<br />

understand some of the implications with certain<br />

development environments highlighted later in<br />

this chapter.<br />

Apartments<br />

process space<br />

Single threaded apartment<br />

(main apartment)<br />

Single threaded apartment<br />

Single threaded apartment<br />

Multithreaded apartment<br />

Thread<br />

neutral<br />

apartment<br />

Think of the SCM (pronounced scum) as the<br />

COM runtime environment. The SCM interacts<br />

with objects, servers, and the operating system<br />

and provides the transparency between clients<br />

and the objects with which they work.<br />

THREADS, APARTMENTS, AND MARSHALLING<br />

A thread is a process flow through an application. There are potentially many<br />

threads within Windows applications. An apartment is a group of threads that<br />

works with contexts within a process. With COM+, a context belongs to one<br />

apartment. There are potentially many types of contexts; security is an example<br />

of a type of context. Before successfully communicating with each other, objects<br />

must have compatible contexts.<br />

COM supports two types of apartments: single-threaded apartments and<br />

multithreaded apartments (MTA). COM+ supports the additional thread-neutral<br />

apartment (TNA). A process can have any number of STAs; each process creates<br />

one STA called the main apartment. Threads that are created as apartments are<br />

placed in an STA. All user interface code is placed in an STA to prevent deadlock<br />

situations. A process can only have one MTA. A thread that is started as<br />

multithreaded is placed in the MTA. The TNA has no threads permanently associated<br />

with it; rather, threads enter and leave the apartment when appropriate.<br />

In-process objects have an entry in the registry, the ThreadingModel, that informs<br />

the COM service control manager (SCM) into which apartment to place the<br />

object. If the object’s requested apartment is compatible with the creator’s apartment,<br />

the object is placed in that apartment; otherwise, the SCM will find or<br />

create the appropriate apartment. If no threading model is defined, the object<br />

will be placed in the main apartment of the process. The ThreadingModel registry<br />

entry can have the following values:<br />

• Apartment. Object must be executed within the STA. Normally used by UI<br />

objects.<br />

• Free. Object must be executed within the MTA. Objects creating threads are<br />

normally placed in the MTA.<br />

• Both. Object is compatible with all apartment types. The object will be created<br />

in the same apartment as the creator.<br />

• Neutral. Objects must execute in the TNA. Used by objects to ensure there is<br />

no thread switch when called from other apartments. This is only available<br />

under COM+.<br />

Marshalling enables a client to make interface function calls to objects in other<br />

apartments transparently. Marshalling can occur between COM apartments on<br />

different machines, between COM apartments in different process spaces, and<br />

between COM apartments in the same process space (STA to MTA, for example).<br />

COM provides a standard marshaller that handles function calls that use automation-compliant<br />

data types (see table below). Nonautomation data types can be<br />

handled by the standard marshaller as long as proxy stub code is generated; otherwise,<br />

custom marshalling code is required.<br />

Chapter 4 • <strong>Developer</strong> environments • 67

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!