COM and Suites (Apartments) 1-Basic knowledge

Source: Internet
Author: User
http://blog.csdn.net/ATField/archive/2007/10/14/1824640.aspx Overviewis COM technology out of date? This sentence is neither right nor wrong. Technically, it is true that COM usage is declining, but intellectually, the idea of COM's interface-oriented is being carried forward by Java and. NET. So we still need to deal with COM. It depends on the area of your work. While Microsoft's platform is slowly moving toward. NET migration, COM is more or less unavoidable in maintaining the original unmanaged code, writing programs that deal with Windows system components, and using the CLR to invoke unmanaged code. It's impossible to deal with COM without talking about suites (apartments). A suite is a very useful concept in COM, but also very difficult to understand, it can be said that many of the problems in COM and suites, understand the suite, from fully understand COM is a step closer, this article will discuss the basic knowledge of the suite and the application in. NET several times. What is a suite (apartments)The suite is a set of mechanisms that COM introduces to simplify the object's support for multithreading to specify ThreadsAnd COM Object Multithreaded features, and provides synchronous support for calls between suites of different features, ensuring that objects of different multithreaded features can be invoked correctly with each other without introducing synchronization problems and simplifying programming (which may actually be more complicated). For example, if an object is written to forget to consider multithreading, or have no time to consider, or do not need to provide support for multithreaded, this time you can designate the object as STA, let COM automatically manage the call to the object, to ensure that the object can be called correctly, Even multi-threaded calls are serialized (called in turn, not simultaneous calls). Conversely, if an object supports multi-threaded invocation, it can be marked as MTA, and COM allows it to be called multithreaded. For a suite, note the following: <!--[if!supportlists]--> 1.     <!--[endif]--> Suite is not a real area, It's a logical concept <!--[if!supportlists]--> 2.     <!--[endif]--> Suite shows the multithreaded nature of the code in the suite, Determines the following <!--[if!supportlists]--> a.     <!--[endif]--> code itself allows single-threaded calls or multi-threaded calls <!--[ If!supportlists]--> b.     <!--[endif]--> code to create a COM object to get a proxy or a raw pointer (about proxy See the later proxy section) <!--[if!supportlists]--> c.     <!--[endif]--> code calls the same set of COM The object is through the original pointer, and the different suites are via proxy <!--[if!supportlists]--> 3.     <!--[endif]--> threads must belong to a suite, This indicates the multithreaded nature of the thread itself, that is, whether the thread calls a COM object in a multithreaded or single-threaded way. For example, if a thread is in an STA, the thread is only suitable for direct calls that support single-threaded calls, the same set of COM objects, other COM objects need to be invoked indirectly via proxy, and what will be referred to after the proxy. At the same time, the suite that the thread is in also determines whether the object itself or proxy is obtained when the object is created. Threads can only belong to one suite at a time or not to a single suite, but the threads may belong to different suites at different times. A typical example is when a thread invokes the CoInitialize and then calls CoUninitialize out of the suite, then calls CoInitialize into another suite, and the thread temporarily enters NTA is an example. Here's what we'll talk about. <!--[if!supportlists]--> 4.     <!--[Endif]-->com objects must also belong to a suite, which determines the multithreaded nature of COM objects, Similar to the above. COM objects are not migrated from one suite to another, and if the suite is released, the fact that the object is released at the same time is particularly important for the STA suite. <!--[if!supportlists]--> 5.     <!--[endif]--> across suites must be passed by proxy, which is the basis for COM guaranteed suites to work. The following proxy section will talk about why this is so   type of Suite (apartments)Common apartments have STA and MTA, and a new suite NTA is introduced in the Win2000. STA is used for single-threaded, MTA is used for multithreading. NTA, however, is called thread-independent (thread-neutral) multithreading. In short, STA, MTA, NTA, see the following table:
  Cross-suite can be executed on any thread threads can belong to the suite multithreading features. A message loop is required. number of apartments in process number of middle-range in Suite
STA No Yes Single Thread Yes Unlimited 1
MTA No Yes Multithreading No 1 Unlimited
NTA Yes No Multithreading No 1 Unlimited
The features, differences, and programming considerations for each suite are explained in detail later in this article. Threads and SuitesThread by calling the CoInitialize/ CoInitializeExEnter the suite and exit the suite via CoUninitialize. Entering the suite may result in the suite being created, and the same couninitialize call will result in the suite being released. The number of calls to CoInitialize and CoUninitialize must match, similar to addref/release. CoInitialize can only enter the STA suite, and CoInitializeEx can enter the different suites through the incoming parameters, the incoming coinit_apartmenthreaded into the STA, and the incoming coinit_multithreaded is entered into the MTA. When the coinitialize/counintiialize is invoked, the thread belongs to the suite, and if the STA is specified, the new STA will always be created, and if the MTA is specified, a new MTA is created if the MTA does not exist. The attentive friend may have noticed that there are 3 kinds of suites mentioned above, so where is NTA running? In fact, a thread does not belong to the NTA, the thread can only temporarily enter the NTA, NTA can only exist objects. Objects and ApartmentsCOM objects always belong to a suite. COM objects in the registry can specify the type of suite the object expects by the ThreadingModel property, valid values are:
Property Value meaning
Main ( default value ) Main STA, which is the first STA created
Apartment Sta
Both STA or MTA can
Free Mta
Neutral NTA
It should be explained that, from the suite point of view, the main STA and other non-main STA is not different, but specifically designated as the main Sta. the relationship between the thread suite and the object SuiteAs you can see, the threads also have a suite, and the object also has a suite, so what is the relationship between the two? This is a relatively confusing problem. In fact, in simple terms, the object's suite setting determines which suite the object is in, and the thread's suite determines the thread's suite. OK, see here you may say, this is not equal to not say. Oh, this is really the most essential difference, however, the other two suites of the setting also determines another point, namely Suites and objects are compatible and are in the same suite。 It's important because it's decided that the pointer to the object returned by CoCreateInstance is the original pointer or Proxy(This is where the process is discussed and the proxy is always outside of the process). For example, if the thread's suite is an STA and the object's suite is also an STA, then the object is created in the STA where the thread is located, whereas if the threads are in the apartment of an STA and the object's suite is an MTA, then the object is created in the unique MTA suite. The thread gets the proxy (proxy) of the object, not the original pointer. The concept of an agent will be mentioned later. There is a table in MSDN that is slightly modified here, listed below:
  Object Suite =main Object Suite =apartment Object Suite =both Object Suite =free Object Suite =neutral
Thread Suite = Main STA Main STA Current Thread Suite Current Thread Suite Mta

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.