472,983 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 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 1546
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.