Groups | Blog | Home
all groups > dotnet interop > august 2005 >

dotnet interop : .NET events do not execute on primary thread


Jared Parsons [MSFT]
8/29/2005 3:44:14 PM
This is one major difference between VB6 and .NET. In VB6 all of the
objects you create are STA COM objects. This is what caused the events to
be marshaled back to the primary thread. .NET objects have no concept of
thread affinitiy [1]. The best way to view a .NET object is that it is a
MTA object that aggregates the free threaded marshaller, (or that it lives
in the thread nuetral apartment).

You will have to manually Marshal these events back to the foreground
thread.

[1] Technically ContextBound objects do have a concept of thread affinity
but that's not relevant in this scenario.

--
Jared Parsons [MSFT]
jaredpar@online.microsoft.com
http://blogs.msdn.com/jaredpar
"This posting is provided "AS IS" with no warranties, and confers no rights"
[quoted text, click to view]

George
8/29/2005 5:01:43 PM
My .NET application handles events from an out-of-proc COM server. These
events do not execute on the app's primary thread, however. Is this a
consequence of COM Interop or can this be controlled? (The .NET app is
configured to run in a STA.) In a VB6 program, the events are executed on
the primary thread. Thanks for the help.

v-phuang NO[at]SPAM online.microsoft.com (
8/30/2005 12:00:00 AM
Hi George,

I think cross-apartment marshaling was to provide the ability to do COM
call when they lives in the different apartment.
So cross-apartment do will have some performance hit, but that is necessary
when we have to do cross-apartment marshaling, because the COM call between
apartments is common. We can not make sure all the COM call occur on the
same thread, that will limit the COM's ability.

Also the .NET Interop will also need some more effort to do the COM/.NET
type converting and parameter passing....

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
George
8/30/2005 7:15:50 PM
Thanks for the explanation. Switching to the primary thread to handle the
event solved the original problem, which was slow execution on in-proc,
apartment-threaded COM server methods. (Roughly 80 times faster on the
primary thread!)

Thinking that cross-apartment marshaling was responsible, I changed the
in-proc server to the free-threaded model, but the timings didn't improve.
Was cross-apartment marshaling a bad theory or is .NET/COM Interop somehow
affecting this? Thanks again for the help.


[quoted text, click to view]

Willy Denoyette [MVP]
9/1/2005 12:00:00 AM
I'm missing something here, you are talking about an out-proc COM server, in
this scenario there is no cross-apartment marshaling involved, all there is
is cross-process marshaling over DCOM which is allways much slower than
in-proc marshaling between incompatible apartments.
Question is what out-proc server are you talking about? Is this a VB6
ActiveX server?

Willy.

[quoted text, click to view]

yhhuang NO[at]SPAM online.microsoft.com
9/4/2005 12:00:00 AM
Hello George,

I am reviewing the issue thread. Do you still have more concerns on it? If
there is any question, please feel free to post here and we will follow up.

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
-http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
p&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
v-phuang NO[at]SPAM online.microsoft.com (
9/5/2005 12:00:00 AM
Hi

Thanks for your updated!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
George
9/5/2005 4:57:35 PM
In handling a COM event from the out-of-proc server, the app calls a method
on an in-proc server. This method executes more slowly when called from the
event handler than it does if called from the app's primary thread. I
suspected marshaling was responsible since the event handler thread
(affiliated with an MTA) was using a COM object created in a STA. Creating
the in-proc COM object in an MTA however, did not improve performance.

[quoted text, click to view]

George
9/5/2005 5:03:30 PM
Hi Yanhong Huang,

Thanks for checking. We have opened a case with Microsoft Support on this
and are awaiting further information.

George


[quoted text, click to view]

yhhuang NO[at]SPAM online.microsoft.com
9/6/2005 10:02:04 AM
Hi George,

Thanks very much for your update. If it is convenient for you, could you
please update the community with the result after you communicate with
support team? Hopefully the community can benefit from it.

Thanks again for participating the community. Have a nice day.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
-http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
p&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button