Threading problems with COM Interop

  • Thread starter Thread starter rajesh.sivakumar
  • Start date Start date
R

rajesh.sivakumar

I am using a third party COM componenet which is not thread safe in a .Net
web application using Interop. When two threads (two requests) are trying to
access the componenet it is giving an error and crashing. It works fine if
the request is made only one at a time. How can I code around this problem
so that only one thread operates on the COM object at one time.

Any help will be greatly appreciated.

Thanks
Rajesh
 
Rajesh,

If you have control over the thread that the COM object runs on create that
thread with ApartmentState to STA. This should allow the COM object to work
properly and create individual instances on separaet threads.

This is likely the only way you will get this to work - STA style COM
objects will get corrupted if called from a free threaded environment if
more than one thread accesses this object. STA will make sure that the COM
object always gets marshalled to the thread that created it.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
 
Make sure you set aspcompat attribute to true in your aspx page
<%@ Page aspcompat="true" ...%>

Willy.
 
Back
Top