请教各位高手,看到了“套间appartment”的概念,但是自己始终没有办法理解!!!。那为高手能给个比较通俗的解释,感激不尽!!!

解决方案 »

  1.   

    套间就是com对象管理器,你可以想象成一个房间(apartment)里住着一些美美(com对象),象你这样的许多家伙(Thread)流着口水想进去看看,我(套间管理人员)在维护秩序,如果只允许一次进去一个,就是单线程套间,如果允许大家一齐进去,就是Muti Thread appartment.
    总之,套间为了解决com的多线程问题。
    开个玩笑,仅供参考,高手赐教。
      

  2.   

    那下面那句话的意思------Threads live in apartments.
    Objects live in apartments. Sometimes an object shares the same apartment as the thread that created it, and sometimes an object lives in a different apartment than the thread that created it.COM currently defines two kinds of apartments: a single-threaded apartment (often abbreviated STA) and a multithreaded apartment (often abbreviated MTA). The names are pretty self-explanatory. A single-threaded apartment houses a single thread, and a multithreaded apartment houses multiple threads. A single process has at most one MTA, but it might have many STAs.
      

  3.   

    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
    // This creates a single threaded apartment(STA)
    CoInitializeEx(NULL, COINIT_MULTITHREADED);
    // This creates/enters a multi-threaded apartment(MTA)Although the COM SDK emphasizes that "apartment" is a logical concept, it actually has great impacts on the infrastructure of COM. It's not merely logically meaningful.
    In fact, you can think of "apartment" as a grouping of threads. Different grouping makes different apartments. Just keep these criteria in mind: One process can have only one MTA, but many STAs; An apartment(STA or MTA) can only exist in one process; If the client and the server are in different apartment (different STA, or different MTA, or one STA, the other MTA), marshalling is needed, although you may not see the marshalling yourself.