Re: Wried problem

Tech-Archive recommends: Speed Up your PC by fixing your registry



Thank you very much. Now I understand what is going on there. I thought
NTA was windows2k only because of the statement in the dialog when you
create ATL objects. :p

I am kinda new to C#. Are there any methods to comparing two objects
across apartments.

Willy Denoyette [MVP] wrote:
"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message
news:%23AFxhM82GHA.1060@xxxxxxxxxxxxxxxxxxxxxxx
|
| "leiz" <leizhao831@xxxxxxxxx> wrote in message
| news:1158625241.204727.214560@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|| If I understand you correctly, as along as I dont use Form and set the
|| main thread to MTA, the result would be true, right?
||
|
| Right.
|
|| I think that the problem is that the event handler is called by some
|| threads created by c# are in different apartment. However, when I print
|| out the ApartmentState in the event handler, it is a MTA, so they
|| should be in same apartment. Therefore, the objects should be same.
||
|
| No they don't. The callback (the Sink interface) is running on a
ThreadPool
| thread which is created by the COM interop layer (through a CCW). The CCW
| aggregates the free threaded marshaler and initializes the thread as NTA
| (Neutral Threaded Apartment) which is always the case when you expose .NET
| classes to COM clients.
| That means that the callback can run on any thread, it has no apartment
| requirements at all. The GetApartmentState returns MTA because it treats
| both as compatible, but I would prefer the more correct NTA instead of
MTA.
| Anyway the context differs from the MTA context.
| But, I guess you have a wrong idea about the references you are comparing
| and what they point at.
| app and m_WordApp are references to an object of type Word.Application,
| however, this is not a reference to a COM object. It's a reference to a
| context object (one per apartment type in the process) that holds a
| hashtable holding RCW's which represents the real IUnknow/IDispatch COM
| object interface.
| So, when you compare app with m_WordApp, you compare whether they use the
| same context, however, when they are equal, that doesn't mean that the COM
| interfaces (the RCW) are the same, they are NOT. And because the RCW's
| aren't the same doesn't mean that the object they refer to are different,
| you see?
| Honestly, I don't see what you are after by comparing these reference
| really.
|
|
| Willy.


Correction, after re-reading I noticed following error in above.


The callback (the Sink interface) is running on a ThreadPool
thread which is created by the COM interop layer (through a CCW).
This should read...

The callback (the Sink interface) is running on a native
thread which is created by the COM interop layer (through a CCW).

Willy.

.



Relevant Pages

  • Re: Wried problem
    ... || main thread to MTA, the result would be true, right? ... || should be in same apartment. ... this is not a reference to a COM object. ... The callback (the Sink interface) is running on a ThreadPool ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Wried problem
    ... || main thread to MTA, the result would be true, right? ... || should be in same apartment. ... this is not a reference to a COM object. ... The callback (the Sink interface) is running on a ThreadPool ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Wried problem
    ... | main thread to MTA, the result would be true, right? ... | should be in same apartment. ... I guess you have a wrong idea about the references you are comparing ... this is not a reference to a COM object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threading problem
    ... MTA and STA are ... In-proc objects have apartment ... FTM means your object is apartment-agnostic - it can ...
    (microsoft.public.win32.programmer.ole)
  • Re: Apartments and threading model
    ... An apartment is a COM abstraction about processes ... An STA is bound to a single thread and is defined as the ... CoInitialize - this is destroyed by the matching call to ... The MTA has very different rules. ...
    (microsoft.public.vc.atl)