473,395 Members | 1,568 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,395 software developers and data experts.

No performance improvement for MultiThreaded call to a VB COM

I have included all the source codes in the attached MyTest.zip
(http://www.codeguru.com/forum/attach...chmentid=11218)

There are three projects:
VBTestCOM project is a apartment threaded DLL, it has one function doing a
stored procedure call. This DLL will be called from C++ multithread.

C++ test project is a ATL multithreaded DLL, it just simple created
multithread, and call VBTestCom's doSPCall function in each thread.

VB client:
It set thread number and start a loop calling C++ DLL's Do function.

You need created a stored procedure in SQL Server NorthWind database like
this:
create PROCEDURE dbo.usp_Test
AS
BEGIN
insert Categories(CategoryName,[Description])
values( 'CatName', convert(varchar(30),getdate(),9))

WAITFOR delay '00:00:00.100'
end
It create a record in Categories and delay 100 milliseconds.

In C++ debug, Change the thread number and you will see no performance
improvement. Trace time in thread like this:
1 Thread
vb function call time: 109
c++ other code time: 62

2 thread
vb function call time: 156
c++ other code time: 62

3 thread
vb function call time: 256
c++ other code time: 62

It's frustrating! Help!

Nov 17 '05 #1
9 1564
Hi wdwedw!
There are three projects:
VBTestCOM project is a apartment threaded DLL, it has one function doing a
stored procedure call. This DLL will be called from C++ multithread.

C++ test project is a ATL multithreaded DLL, it just simple created
multithread, and call VBTestCom's doSPCall function in each thread.

It's frustrating! Help!


If the VBTestCOM project is apartment-threaded (that means:
single-threaded) then *all* calls to this COM-object is serialized.

So the behaviour is by design.
If you want to improve the performance, then switch the VBTestCOM
project to both or multi-threaded.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #2

"Jochen Kalmbach [MVP]" <no********************@holzma.de> wrote in message
news:OU*************@TK2MSFTNGP09.phx.gbl...
Hi wdwedw!
There are three projects:
VBTestCOM project is a apartment threaded DLL, it has one function doing
a stored procedure call. This DLL will be called from C++ multithread.

C++ test project is a ATL multithreaded DLL, it just simple created
multithread, and call VBTestCom's doSPCall function in each thread.

It's frustrating! Help!
If the VBTestCOM project is apartment-threaded (that means:
single-threaded) then *all* calls to this COM-object is serialized.

So the behaviour is by design.
If you want to improve the performance, then switch the VBTestCOM project
to both or multi-threaded.


Which is not possible using VB, VB6 can only produce Single Threaded
Apartment (STA) or Single (that is instantiated on the "main apartment") COM
DLL's.

Willy. --
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 17 '05 #3
This is quite normal, your VB object is a STA object, and you initialize the
thread to enter the MTA. The result is that each call as to be marshaled
between the MTA and the STA where the object lives, this kill your
performance. Initialize your apartment for STA and try again.

Willy.
"wdwedw" <wd****@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
I have included all the source codes in the attached MyTest.zip
(http://www.codeguru.com/forum/attach...chmentid=11218)

There are three projects:
VBTestCOM project is a apartment threaded DLL, it has one function doing a
stored procedure call. This DLL will be called from C++ multithread.

C++ test project is a ATL multithreaded DLL, it just simple created
multithread, and call VBTestCom's doSPCall function in each thread.

VB client:
It set thread number and start a loop calling C++ DLL's Do function.

You need created a stored procedure in SQL Server NorthWind database like
this:
create PROCEDURE dbo.usp_Test
AS
BEGIN
insert Categories(CategoryName,[Description])
values( 'CatName', convert(varchar(30),getdate(),9))

WAITFOR delay '00:00:00.100'
end
It create a record in Categories and delay 100 milliseconds.

In C++ debug, Change the thread number and you will see no performance
improvement. Trace time in thread like this:
1 Thread
vb function call time: 109
c++ other code time: 62

2 thread
vb function call time: 156
c++ other code time: 62

3 thread
vb function call time: 256
c++ other code time: 62

It's frustrating! Help!

Nov 17 '05 #4
Willy,

Is that means the VB object can only lives in STA? even though create it
from MTA. Is there no ways to improve performannce for VB object with
Multithread? What does 'Initialize your apartment for STA' mean and how?

Thanks
wdwedw

"Willy Denoyette [MVP]" wrote:
This is quite normal, your VB object is a STA object, and you initialize the
thread to enter the MTA. The result is that each call as to be marshaled
between the MTA and the STA where the object lives, this kill your
performance. Initialize your apartment for STA and try again.

Willy.
"wdwedw" <wd****@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
I have included all the source codes in the attached MyTest.zip
(http://www.codeguru.com/forum/attach...chmentid=11218)

There are three projects:
VBTestCOM project is a apartment threaded DLL, it has one function doing a
stored procedure call. This DLL will be called from C++ multithread.

C++ test project is a ATL multithreaded DLL, it just simple created
multithread, and call VBTestCom's doSPCall function in each thread.

VB client:
It set thread number and start a loop calling C++ DLL's Do function.

You need created a stored procedure in SQL Server NorthWind database like
this:
create PROCEDURE dbo.usp_Test
AS
BEGIN
insert Categories(CategoryName,[Description])
values( 'CatName', convert(varchar(30),getdate(),9))

WAITFOR delay '00:00:00.100'
end
It create a record in Categories and delay 100 milliseconds.

In C++ debug, Change the thread number and you will see no performance
improvement. Trace time in thread like this:
1 Thread
vb function call time: 109
c++ other code time: 62

2 thread
vb function call time: 156
c++ other code time: 62

3 thread
vb function call time: 256
c++ other code time: 62

It's frustrating! Help!


Nov 17 '05 #5
wdwedw wrote:
Willy,

Is that means the VB object can only lives in STA? even though
create it from MTA. Is there no ways to improve performannce for VB
object with Multithread? What does 'Initialize your apartment for
STA' mean and how?


In your C++ code, for each new thread call CoInitializeEx and pass
COINIT_COINIT_APARTMENTTHREADED as the second parameter. That will create a
new STA for each thread. A VB-created object can live in any one of those
apartments.

-cd
Nov 17 '05 #6

"wdwedw" <wd****@discussions.microsoft.com> wrote in message
news:B0**********************************@microsof t.com...
Willy,

Is that means the VB object can only lives in STA? even though create it
from MTA. Is there no ways to improve performannce for VB object with
Multithread? What does 'Initialize your apartment for STA' mean and how?


STA objects are single threaded but they can live in multiple STA's and each
thread can 'enter' one single STA at a time.
Each thread will create/call the object in it's owning STA without the need
to marshal and serialize the call, beware that static (shared) variables in
your VB COM objects are shared amongst the different thread instances, so
their accesses must be synchronized anyway. I've seen many applications
using VB COM objects in multithreaded applications breaking because of this
thread-unsafe design.
Willy.


Nov 17 '05 #7
Thanks Carl and Willy.

When I created a apartment for each thread, I did see the performance was
improved!

Two more questions:
1) Is static (shared) variables means public variables in Modules? because
they shared in VB project.
2) My ATL object implemented connectionpoint and work fine before, after I
create a apartment for each thread and fire the event, I got a First-chance
exception during pDispatch->Invoke() in the Fire_ServerFuncDone(LONG nReturn)
function. What's wrong?
Thanks
wdwedw
"Willy Denoyette [MVP]" wrote:

"wdwedw" <wd****@discussions.microsoft.com> wrote in message
news:B0**********************************@microsof t.com...
Willy,

Is that means the VB object can only lives in STA? even though create it
from MTA. Is there no ways to improve performannce for VB object with
Multithread? What does 'Initialize your apartment for STA' mean and how?


STA objects are single threaded but they can live in multiple STA's and each
thread can 'enter' one single STA at a time.
Each thread will create/call the object in it's owning STA without the need
to marshal and serialize the call, beware that static (shared) variables in
your VB COM objects are shared amongst the different thread instances, so
their accesses must be synchronized anyway. I've seen many applications
using VB COM objects in multithreaded applications breaking because of this
thread-unsafe design.
Willy.


Nov 17 '05 #8
I think I got the answer for my second question: I need to fire the event
from a different thread:
http://support.microsoft.com/kb/q280512/

"wdwedw" wrote:
Thanks Carl and Willy.

When I created a apartment for each thread, I did see the performance was
improved!

Two more questions:
1) Is static (shared) variables means public variables in Modules? because
they shared in VB project.
2) My ATL object implemented connectionpoint and work fine before, after I
create a apartment for each thread and fire the event, I got a First-chance
exception during pDispatch->Invoke() in the Fire_ServerFuncDone(LONG nReturn)
function. What's wrong?
Thanks
wdwedw
"Willy Denoyette [MVP]" wrote:

"wdwedw" <wd****@discussions.microsoft.com> wrote in message
news:B0**********************************@microsof t.com...
Willy,

Is that means the VB object can only lives in STA? even though create it
from MTA. Is there no ways to improve performannce for VB object with
Multithread? What does 'Initialize your apartment for STA' mean and how?


STA objects are single threaded but they can live in multiple STA's and each
thread can 'enter' one single STA at a time.
Each thread will create/call the object in it's owning STA without the need
to marshal and serialize the call, beware that static (shared) variables in
your VB COM objects are shared amongst the different thread instances, so
their accesses must be synchronized anyway. I've seen many applications
using VB COM objects in multithreaded applications breaking because of this
thread-unsafe design.
Willy.


Nov 17 '05 #9
Inline

Willy.

"wdwedw" <wd****@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
Thanks Carl and Willy.

When I created a apartment for each thread, I did see the performance was
improved!

Two more questions:
1) Is static (shared) variables means public variables in Modules?
because
they shared in VB project.
Exactly.
2) My ATL object implemented connectionpoint and work fine before, after I
create a apartment for each thread and fire the event, I got a
First-chance
exception during pDispatch->Invoke() in the Fire_ServerFuncDone(LONG
nReturn)
function. What's wrong?
Thanks
So it looks like have a ATL COM object AND a VB6 COM object, this is not
what you told us in the initial posting.
Who's creating the ATL object instance, who's creating the VB instance what
apartments are they in?
wdwedw
"Willy Denoyette [MVP]" wrote:

"wdwedw" <wd****@discussions.microsoft.com> wrote in message
news:B0**********************************@microsof t.com...
> Willy,
>
> Is that means the VB object can only lives in STA? even though create
> it
> from MTA. Is there no ways to improve performannce for VB object with
> Multithread? What does 'Initialize your apartment for STA' mean and
> how?
>


STA objects are single threaded but they can live in multiple STA's and
each
thread can 'enter' one single STA at a time.
Each thread will create/call the object in it's owning STA without the
need
to marshal and serialize the call, beware that static (shared) variables
in
your VB COM objects are shared amongst the different thread instances, so
their accesses must be synchronized anyway. I've seen many applications
using VB COM objects in multithreaded applications breaking because of
this
thread-unsafe design.
Willy.


Nov 17 '05 #10

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

Similar topics

7
by: qvx | last post by:
Hi all, I have a performance problem in my app. It is a poor man's version of OCR app. I started this app as a prototype before implementing it in C++. But now, after I have a working copy in...
1
by: Peter Arrenbrecht Opus | last post by:
Hello IBM I think that one could improve the performance of DB2 UDB v7.2's stored procedure resolution. Here's what DB2 normally does: SELECT A.PROCSCHEMA, A.PROCNAME, A.PARMNAME,...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: Bill Thorne | last post by:
We have a COM object that has been wrappered for use in .NET and which can make calls which can take a while to execute. These are mainframe integration calls that might perform a lot of data...
3
by: groups | last post by:
Hi all, I've recently ported a rather large C application to run multithreaded. A few functions have seriously deteriorated in performance, in particular when accessing a rather large global...
12
by: Lloyd Dupont | last post by:
I have an application which use has a DLL with 100+ (auto-generated) Managed C++ wrapper around some native API. Compare to a purely version my application has some performance issue and I just...
3
by: yonil | last post by:
Over the years of using C++ I've begun noticing that freestore management functions (malloc/free) become performance bottlenecks in complex object-oriented libraries. This is usually because these...
10
by: colin | last post by:
Hi, I profile my code and find its spending a lot of time doing implicit conversions from similar structures. the conversions are mainly things like this class Point { implicit conversion...
1
by: nataraj2502 | last post by:
Hi, I have just configured my C# multi-threaded application from 32 bit to 64 bit platform for getting more memory support. But my application runs up to 40 - 50% slower as compared to 32 bit...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.