Monday, January 18, 2016
single-threaded apartment
/*Rules for single-threaded apartments are simple, but it is important to follow them carefully:
Every object should live on only one thread (within a single-threaded apartment).
Marshal all pointers to objects when passing them between apartments./*传统线程可以访问同进程内的资源*/
Each single-threaded apartment must have a message loop to handle calls from other processes and apartments within the same process. Single-threaded apartments without objects (client only) also need a message loop to dispatch broadcast sendmessages that some applications use.
DLL-based or in-process objects do not call the COM initialization ;Each thread of a client process or out-of-process server must call CoInitialize, or call CoInitializeEx and specify COINIT_APARTMENTTHREADED for the dwCoInit parameter。The main apartment is the thread that calls CoInitializeEx first.
DLL-based or in-process objects register their threading model with the ThreadingModel named-value under the InprocServer32 key in the registry. Apartment-aware objects must also write DLL entry points carefully. */
COM creates a hidden window using the Windows class "OleMainThreadWndClass" in each single-threaded apartment. A call to an object is received as a window message to this hidden window. When the object's apartment retrieves and dispatches the message, the hidden window will receive it. The window procedure will then call the corresponding interface method of the object.
Single-threaded apartments can implement IMessageFilter 。
综上,单线程套间本质上是一个类(接口),保存着一个窗口句柄、一个线程(线程先初始化com库[就是注册],然后有消息loop)、0或多个组件对象、客户线程(也需要在com注册),该类(接口)可以进一步实施IMessageFilter接口。
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment