Groups | Blog | Home
all groups > dotnet remoting > october 2006 >

dotnet remoting : STA Threads



Pranav
10/18/2006 10:32:47 PM
Hello All,

I have an application server that works with legacy com components with
Apartment threading model. The components take very long to initialize
and I store them in a global collection once created.

All the remoting threads (from default threadpool) are MTA threads. So
when a request comes for MTA thread to create a COM component with
Apartment model, it creates an STA and creates the COM object in that
apartment. Subsequent calls also use the same STA if available and all
the objects get cached in one apartment.

This defeats the whole purpose of having this server.

So, my question is, is there a way to create a threadpool of STA
threads which will handle this?

If this description is confusing, please take a look at this brief
article about the same problem in ASPX pages and they have a solution.
I have the same problem in remoting server.

http://msdn.microsoft.com/msdnmag/issues/06/10/WickedCode/

Thanks a lot for your time.

- Pranav
Pranav
10/19/2006 12:29:44 PM
If it is possible in remoting, then I am all set. But I don't think
so.. because the calls are handled by .net threadpool which are all
MTA. Once the thread has started you can not make it STA.

Also I am caching the COM objects in a global collection, because the
creation is very expensive. So when the next calls come for two
different COM objects, created by the same STA, they get queued on one
thread and the performance goes down.

I tried creating my own STA threads after receiving the call to create
object. I can do that and the object creation works fine but as soon as
the thread goes out of scope, the chached COM object in global
collection also dies and I get a "disconnected from underlying RCW"
error next time I try to access those objects.

What I need is STA threads creating the COM objects and hanging in
there for its lifetime..

[quoted text, click to view]
Günter Prossliner
10/19/2006 2:27:58 PM
Hello!

[quoted text, click to view]


[quoted text, click to view]

In ASP.Net you have no choise other than doing something like it has been
done in this MSDN Article. Because your Application depends on the
functionality and restrictions witch are provided by ASP.Net

What the article describes as a way to let .asmx WebService run in STA
Threads. In your Remoting Scenario you are free to use STA by default,
aren't you?


GP

Günter Prossliner
10/20/2006 12:00:00 AM
Hi Pranav!

[quoted text, click to view]

It seems like that there is no way use an STA from the ThreadPool.

[quoted text, click to view]

No, this is not possible (CoInitializeEx cannot be called twice).


GP

TDC
10/20/2006 6:17:39 AM
Why did you let the STA threads go out of scope?

You shoudl be storing some object in the global collection that *both*
has a reference to the COM object and to the thread that drives it.
That should keep them in scope, right?

Tom

[quoted text, click to view]
AddThis Social Bookmark Button