Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2005

Tip: Looking for answers? Try searching our database.

.NET events do not execute on primary thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George - 29 Aug 2005 23:01 GMT
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.
Jared Parsons [MSFT] - 29 Aug 2005 23:44 GMT
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.

Signature

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"

> 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.
George - 31 Aug 2005 01:15 GMT
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.

> 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
[quoted text clipped - 15 lines]
>> on
>> the primary thread.  Thanks for the help.
"Peter Huang" [MSFT] - 31 Aug 2005 03:47 GMT
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

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Willy Denoyette [MVP] - 01 Sep 2005 17:04 GMT
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.

> Thanks for the explanation.  Switching to the primary thread to handle the
> event solved the original problem, which was slow execution on in-proc,
[quoted text clipped - 26 lines]
>>> on
>>> the primary thread.  Thanks for the help.
George - 05 Sep 2005 22:57 GMT
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.

> 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
[quoted text clipped - 35 lines]
>>>> on
>>>> the primary thread.  Thanks for the help.
Yan-Hong Huang[MSFT] - 05 Sep 2005 04:09 GMT
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.
George - 05 Sep 2005 23:03 GMT
Hi Yanhong Huang,

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

George

> Hello George,
>
[quoted text clipped - 15 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 06 Sep 2005 04:48 GMT
Hi

Thanks for your updated!

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Yan-Hong Huang[MSFT] - 06 Sep 2005 11:02 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.