Threading problems with COM Interop

  • Thread starter rajesh.sivakumar
  • 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
 
R

Rick Strahl [MVP]

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/
 
W

Willy Denoyette [MVP]

Make sure you set aspcompat attribute to true in your aspx page
<%@ Page aspcompat="true" ...%>

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top