473,425 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

COM Object performance in different Threads

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
Nov 17 '05 #1
2 3822
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]
- mv*@spam.guard.caspershouse.com

"Ben Childs" <bchilds@wpidotedu> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
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

Nov 17 '05 #2

"Ben Childs" <bchilds@wpidotedu> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
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


I see two fundamental problems here.
When you create an instance of a COM object in the main (UI) thread , your
COM methods will execute on the UI thread, irrespective the thread that is
calling a method.
When you calls a method from the UI thread itself, the call is said to be a
direct call, however, when called from another thread (passing the reference
to the objects CCW to the other thread) the call must be marshaled to the UI
thread, this marshaling is done by posting windows messages to the UI
thread's message queue where they are picked up and dispatched to you COM
method which executes the call obviously on the UI thread. The marshalin
overhead make the call somewhat slower, but in general this is not such an
issue.
However, in your scenario, where you are updating the UI from the auxiliary
thread (using Control.BeginInvoke I hope, but I'm afraid you don't) things
get even more slower as the UI thread has to update the UI AND run the
analysis code, in the mean time your auxiliary thread is doing almost
nothing.

One possible solution for this is to:
- create an aux. thread,
- initialize this thread to enter a STA,
- start the thread,
- create an instance of your COM object on this thread and call the COM
methods from here,
- marshal the calls to the UI thread by calling Control.BeginInvoke to
update the UI.

Willy.

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Alfonso Morra | last post by:
This may be considered as OT since the C++ Standard says not one word about threads. Nevertheless, C++ is routinely and widely used to write solid multithreaded code. I wondered if anyone has...
5
by: Sunny | last post by:
Hi, I have to implement client/server application. The client have to instaniate an remoting object via http and pass some auth info. If the auth is OK, the client should invoke a method (or...
4
by: Fabian H?rle | last post by:
Hi, I have an STA COM object and I need to access it from different threads. I get the obligatory "QueryInterface for interface xyz failed." exception. Is there any possibility apart from using...
16
by: Elad | last post by:
Hi, I have an application that is made up of several executables. I need all these executables to use the same instance of an object. What is the best, most efficient way to approach this? ...
6
by: Dave | last post by:
I have a service that has 6 different threads. Each thread has a timer on it that elapses at about the same time (if not the same time). When the timer elapses I am trying to log a message by...
9
by: PaowZ | last post by:
Hello there! I've tried to gather some infos about php object performance. I got started developing an app using php object and have the worst requests time I ever seen. I mean, once all my...
0
by: Leo Jay | last post by:
I'd like to read and write the same socket in different threads. one thread is only used to read from the socket, and the other is only used to write to the socket. But I always get a 10022...
0
by: Jean-Paul Calderone | last post by:
On Sat, 23 Aug 2008 02:25:17 +0800, Leo Jay <python.leojay@gmail.comwrote: No - it's just what I said. create_socket creates one socket and passes it to read_socket and write_socket. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.