Single threaded class for COM interop

Discussion in 'ASP .Net' started by Rob, Jul 13, 2004.

  1. Rob

    Rob Guest

    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.
    Rob, Jul 13, 2004
    #1
    1. Advertising

  2. Rob

    bruce barker Guest

    no, if a class is not thread safe, it has no way of enforcing it, it up to
    the caller. the 3 methods you list are for ensuring the caller (a thread)
    does not do a thread switch.

    your solution is the correct one, except, that the stubs should only create
    one STA thread, then switch to that thread to make the call, unless the com
    object is completly stateless.

    -- bruce (sqlwork.com)


    "Rob" <> wrote in message
    news:...
    > 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.
    bruce barker, Jul 13, 2004
    #2
    1. Advertising

  3. Rob

    Rob Guest

    Thanks Bruce.

    "bruce barker" <> wrote in message news:<#>...
    > no, if a class is not thread safe, it has no way of enforcing it, it up to
    > the caller. the 3 methods you list are for ensuring the caller (a thread)
    > does not do a thread switch.
    >
    > your solution is the correct one, except, that the stubs should only create
    > one STA thread, then switch to that thread to make the call, unless the com
    > object is completly stateless.
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "Rob" <> wrote in message
    > news:...
    > > 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.
    Rob, Jul 14, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF1bA==?=

    how to wait for object with single threaded application

    =?Utf-8?B?UGF1bA==?=, Nov 10, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    3,126
    =?Utf-8?B?UGF1bA==?=
    Nov 10, 2004
  2. Tsahi Levent-Levi
    Replies:
    0
    Views:
    2,010
    Tsahi Levent-Levi
    Aug 15, 2003
  3. Rick
    Replies:
    1
    Views:
    549
    Michael Borgwardt
    Nov 17, 2003
  4. Martin Ink

    Starting .exe "single threaded"

    Martin Ink, Oct 3, 2004, in forum: C++
    Replies:
    3
    Views:
    393
    Tom Widmer
    Oct 5, 2004
  5. Klaus
    Replies:
    1
    Views:
    304
    Joona I Palaste
    Dec 9, 2004
Loading...

Share This Page