Single threaded class for COM interop

From: Rob (robsomething_at_hotmail.com)
Date: 07/13/04


Date: 13 Jul 2004 07:36:36 -0700

Hi.

I'm having difficulty calling a 3rd party COM object that must be
called in the apartment (single) threaded model. As far as I know,
there are 3 ways todo this:

1 - use "aspcompat=true" for aspx pages
2 - use [STAThread] attribute for windows/console applications
3 - use System.Thread.CurrentThread.ApartmentState =
ApartmentState.STA

The problem is that the COM object is only called through a business
layer module. This business layer consists of a number of classes that
encapsulate the calls to the COM object. Therefore, option 1 and 2 are
not applicable and option 3 does not work since the threading model
has already been defined even before the class initializer is called.

One solution that I've come up with is calling a 'stub' method for
each method in the business layer. This 'stub' will declare a new
thread, set the apartment model to STA, then start the thread. This is
tedious because every method will require a 'stub' and all parameters
(in and out) must be handled through private class members. I can't
imagine that this is the proper way of doing things.

So, my question is: Is there a way to make the entire class
single-threaded by default (similar to [STAThread] for a windows app)?

Any help would be greatly appreciated.

Rob.



Relevant Pages

  • Re: Single threaded class for COM interop
    ... the 3 methods you list are for ensuring the caller ... > called in the apartment threaded model. ... > each method in the business layer. ... This 'stub' will declare a new ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Threading model
    ... 'Apartment threaded' is the norm for DLLs/OCXs. ... from the DLL are created in apartment associated with the calling thread. ... The first STA apartment created in a process is also ... > What is the difference between apartment threaded model and single ...
    (microsoft.public.vb.general.discussion)