Re: COM Object performance in different Threads



Ben,

COM objects have apartment affinity. By default, the UI thread (the
main thread in your .NET application) is a STA thread, and when you create
your COM object, it is associated with that apartment (it is an STA thread
because of the STAThread attribute attached to your main method).

Now, when you create your new thread, by default, it doesn't live in a
COM apartment. When you make the first call to COM interop, if you havent
set the ApartmentState property on the Thread to a value, it will place the
thread in the multithreaded apartment. Now, when you make calls to the COM
object from that thread, if you marshaled the reference correctly (which you
are not, I am sure, and I don't believe COM interop does this by default),
then all calls will be made through a proxy so the call will be executed on
the correct thread.

In the end, you are either going to have to marshal the reference
correctly (through the use of the global interface table, and a lot of
interop), or create the COM objects on the thread they are going to be used
(and make sure to set the ApartmentState property of the thread to STA
BEFORE you create any COM objects on that new thread).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Ben Childs" <bchilds@wpidotedu> wrote in message
news:%23UFJquPcFHA.720@xxxxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> I am writing an application in C# (VS.NET 2002) that runs performance
> intensive analyses on data sets. In order to keep the UI responsive I have
> created a seperate thread to do the analysis.
>
> I am using my own DLLS to load the data sets from files and run the
> analyses. But I have run into a bit of a problem. If I load the data set
> in the main thread and then run the analysis in the seperate thread the
> analysis takes about 20 times longer than usual. In addition when I try to
> display the results in the gui they take forever to display. So it seems
> that accessing COM objects from different threads than they were created
> in is really slow. Is there any way around this?
>
> One solution for me would be to create a thread that handled all of the
> com objects, however this would be a lot of work and I was wondering if
> there was a simple solution.
>
> Thanks,
>
> -Ben Childs


.



Relevant Pages

  • Re: [STAThread]
    ... If you need to create 'apartment' type objects, make sure you have a pumping ... STA thread in your process, ... Yep, but it's not that difficult to remember the basic rules, if you care, ... and can be used in the MTA. ...
    (microsoft.public.dotnet.languages.csharp)
  • GC calls in for MTA / STA thread
    ... of convincing answers. ... As per the developer i was talking to, these calls are kind of important for STA threads, since for them GC thread can't enter the apartment for cleansing, however for an MTA thread apartment, this is not the case as GC can enter the apartment, but frankly saying it's ... There's no way GC can clean the memory allocated to STA thread objects, since even these calls leads to GC thread invocation and i think there n number of STA applications in the real world, which is the default. ... One more thing if for a given code executed on a thread and finalizer is implemented, then while GC carries out finalization process, does it need to enter the thread apertment, as in that case again STA thread will be dealing with same issue. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: GC calls in for MTA / STA thread
    ... So you are saying that if you have a thread that is an STA thread, ... > making such calls from a STA / MTA thread. ... > cleansing, however for an MTA thread apartment, this is not the case as ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Multi-Threading & Performance Questions
    ... > this impact performance as compared to Thread "B" instantiating object ... By interop, I assume you mean COM interop. ... apartment state of the thread before you use a COM object. ... > As I add "Calculation worker threads" Throughput Performance increases ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Subform variable reference in Query not working
    ... I did not know about the LimitToList property being set to No. ... However the first visible column is NOT the bound column. ... Apartment number. ... If I change my continuous input form into a continuous display, ...
    (microsoft.public.access.formscoding)