473,791 Members | 3,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MultiThreading an ActiveX DLL call...

Hello,

Is there a good way to call a big time-consumming function from an
ActiveX DLL (interoped) through a thread without blocking the main UI ?

Here are the details :

I have a class CInteropCall encapsulating a call to a visual basic
ActiveX DLL function named FillAlloc_Array Struct_Double() , which is
allocating a big struct array an fills it using a inner loop
(for i = 1 to 300000...)

I'm calling this BigProcess through a thread from the main UI
like this :

CInteropCall MyThreadProcess = new CInteropCall();
Thread m_WorkerThread;
m_WorkerThread = new Thread(new ThreadStart(MyT hreadProcess.Te stMe));
m_WorkerThread. IsBackground = true;
m_WorkerThread. Name = "ThreadLow" ;
m_WorkerThread. Priority = System.Threadin g.ThreadPriorit y.Lowest;
m_WorkerThread. Start();
Here is the TestMe() method from the CInteropCall class which is calling
the function through an instance of a VB6 Class :
public void TestMe()
{
AX_DLL.FillAllo c_ArrayStruct_D ouble(3000000, ref array_struct_do uble);
}
The problem is :
The main UI is blocked when starting the thread, and from time to time
during the thread execution.

Solutions tested :
- adding a Sleep(1) in the inner loop of the vb6 function
It does not change a lot
- adding a DoEvents() in the vb6 function
It does not change a lot but slows down the thread execution.
The same big loop executing from a c# function does not block the main
UI (even without sleep(1)...), the UI remains very smooth.
Any idea to make an interoped vb6 activex call 'UI thread friendly' ?
(or maybe there is no way to do it...)

Regards,
Cybertof.
Nov 17 '05 #1
20 3979

"Cybertof" <cy************ @ifrance.com> wrote in message
news:MP******** *************** *@news.wanadoo. fr...
Hello,

Is there a good way to call a big time-consumming function from an
ActiveX DLL (interoped) through a thread without blocking the main UI ?

Here are the details :

I have a class CInteropCall encapsulating a call to a visual basic
ActiveX DLL function named FillAlloc_Array Struct_Double() , which is
allocating a big struct array an fills it using a inner loop
(for i = 1 to 300000...)

I'm calling this BigProcess through a thread from the main UI
like this :

CInteropCall MyThreadProcess = new CInteropCall();
Thread m_WorkerThread;
m_WorkerThread = new Thread(new ThreadStart(MyT hreadProcess.Te stMe));
m_WorkerThread. IsBackground = true;
m_WorkerThread. Name = "ThreadLow" ;
m_WorkerThread. Priority = System.Threadin g.ThreadPriorit y.Lowest;
m_WorkerThread. Start();
Here is the TestMe() method from the CInteropCall class which is calling
the function through an instance of a VB6 Class :
public void TestMe()
{
AX_DLL.FillAllo c_ArrayStruct_D ouble(3000000, ref array_struct_do uble);
}
The problem is :
The main UI is blocked when starting the thread, and from time to time
during the thread execution.

Solutions tested :
- adding a Sleep(1) in the inner loop of the vb6 function
It does not change a lot
- adding a DoEvents() in the vb6 function
It does not change a lot but slows down the thread execution.
The same big loop executing from a c# function does not block the main
UI (even without sleep(1)...), the UI remains very smooth.
Any idea to make an interoped vb6 activex call 'UI thread friendly' ?
(or maybe there is no way to do it...)

Regards,
Cybertof.


You should initialize your (m_WorkerThread ) thread to enter an STA. The way
you do it now results in your COM object to run on the main UI thread.

...
m_WorkerThread. ApartmentState = ApartmentState. STA;
m_WorkerThread. Start();
....

Willy.

Nov 17 '05 #2
In article <ej************ *@TK2MSFTNGP10. phx.gbl>,
wi************* @telenet.be says...
You should initialize your (m_WorkerThread ) thread to enter an STA. The way
you do it now results in your COM object to run on the main UI thread. ..
m_WorkerThread. ApartmentState = ApartmentState. STA;
m_WorkerThread. Start();
...

Hi Willy,

That's exactly what I do not want...
I want the thread no to disturb the main UI thread at all.
Cybertof.
Nov 17 '05 #3

"Cybertof" <cy************ @ifrance.com> wrote in message
news:MP******** *************** *@news.wanadoo. fr...
In article <ej************ *@TK2MSFTNGP10. phx.gbl>,
wi************* @telenet.be says...

I want the thread no to disturb the main UI thread at all.

Sure, but YOU DO disturb the UI by NOT initializing the background thread to
enter an STA, note this is not the UI thread's apartment but a newly created
apartment.
If you don't initialize your backround thread to enter an STA, the AX object
will enter the UI thread's apartment and calls to methods on this object
will run on the UI thread.

Hope it's clear now.

Willy.

Nov 17 '05 #4
In article <uK************ **@TK2MSFTNGP09 .phx.gbl>,
wi************* @telenet.be says...

Sure, but YOU DO disturb the UI by NOT initializing the background thread to
enter an STA, note this is not the UI thread's apartment but a newly created
apartment.
If you don't initialize your backround thread to enter an STA, the AX object
will enter the UI thread's apartment and calls to methods on this object
will run on the UI thread.

Hope it's clear now.

Willy.


Thanks, it's more clear.

And should the VB6 ActiveX be compiled with
'Single Threaded' or 'Apartment Threaded' ?
Regards,
Cybertof.
Nov 17 '05 #5

"Cybertof" <cy************ @ifrance.com> wrote in message
news:MP******** *************** *@news.wanadoo. fr...
In article <uK************ **@TK2MSFTNGP09 .phx.gbl>,
wi************* @telenet.be says...

Sure, but YOU DO disturb the UI by NOT initializing the background thread
to
enter an STA, note this is not the UI thread's apartment but a newly
created
apartment.
If you don't initialize your backround thread to enter an STA, the AX
object
will enter the UI thread's apartment and calls to methods on this object
will run on the UI thread.

Hope it's clear now.

Willy.


Thanks, it's more clear.

And should the VB6 ActiveX be compiled with
'Single Threaded' or 'Apartment Threaded' ?
Regards,
Cybertof.


Apartment.

Willy.
Nov 17 '05 #6
In article <#E************ *@tk2msftngp13. phx.gbl>,
wi************* @telenet.be says...

"Cybertof" <cy************ @ifrance.com> wrote in message
news:MP******** *************** *@news.wanadoo. fr...
In article <uK************ **@TK2MSFTNGP09 .phx.gbl>,
wi************* @telenet.be says...


Apartment.

Willy.


STA...Appartmen t...no changes.

I think it's not a problem of thread :

A very very long time is spent during parameter passing.

My array is passe byref, I don't understand why it takes so long time.
Is there any method to pass efficiently an array as a parm to a VB6
ActiveX DLL function ?
Regards,
Cybertof.
Nov 17 '05 #7

"Cybertof" <cy************ @ifrance.com> wrote in message
news:MP******** *************** *@news.wanadoo. fr...
In article <#E************ *@tk2msftngp13. phx.gbl>,
wi************* @telenet.be says...

"Cybertof" <cy************ @ifrance.com> wrote in message
news:MP******** *************** *@news.wanadoo. fr...
> In article <uK************ **@TK2MSFTNGP09 .phx.gbl>,
> wi************* @telenet.be says...


Apartment.

Willy.


STA...Appartmen t...no changes.

I think it's not a problem of thread :

A very very long time is spent during parameter passing.

My array is passe byref, I don't understand why it takes so long time.
Is there any method to pass efficiently an array as a parm to a VB6
ActiveX DLL function ?
Regards,
Cybertof.


Are you sure that you create the instance of your COM object, and call the
COM method in the same thread procedure?
Failing to do so will incur thread marshaling overhead!

So the STA threading rules are simple;
- initialize your thread to enter an STA.
- call the COM object's methods on the same thread as the one that created
the object instance.

Willy.


Nov 17 '05 #8
In article <uk************ **@TK2MSFTNGP14 .phx.gbl>,
wi************* @telenet.be says...
Are you sure that you create the instance of your COM object, and call the
COM method in the same thread procedure?
Failing to do so will incur thread marshaling overhead!

So the STA threading rules are simple;
- initialize your thread to enter an STA.
- call the COM object's methods on the same thread as the one that created
the object instance.

Willy.


Yes, I'm sure, everything is done in the same thread.

As another example, I have removed the 2nd thread creation, and made the
call directly behing a button_click in the main UI Thread.

It's slow when passing the array to the VB6 AX Function, and returning
from the function.

It seems like the array is entirely copied instead of beeing passed
byref. (even if the ref keyword is used during the call...)
Any idea ?
Nov 17 '05 #9

"Cybertof" <cy************ @ifrance.com> wrote in message
news:MP******** *************** *@news.wanadoo. fr...
In article <uk************ **@TK2MSFTNGP14 .phx.gbl>,
wi************* @telenet.be says...
Are you sure that you create the instance of your COM object, and call
the
COM method in the same thread procedure?
Failing to do so will incur thread marshaling overhead!

So the STA threading rules are simple;
- initialize your thread to enter an STA.
- call the COM object's methods on the same thread as the one that
created
the object instance.

Willy.


Yes, I'm sure, everything is done in the same thread.

As another example, I have removed the 2nd thread creation, and made the
call directly behing a button_click in the main UI Thread.

It's slow when passing the array to the VB6 AX Function, and returning
from the function.

It seems like the array is entirely copied instead of beeing passed
byref. (even if the ref keyword is used during the call...)
Any idea ?

Ok, don't mix two different issues, speed and UI responsiveness. Your
initial issue was that the UI thread was blocked for the duration of the
call, the solution for this was to move this to a background thread, right?
Well, is this issue solved?
If it is, we can have a look at the speed issue, a few questions come to
mind though, how's the speed compared to a VB6 client calling into your
object? Could you post some code?

Willy.


Nov 17 '05 #10

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

Similar topics

15
9734
by: Mark Sisson | last post by:
Hey all. I've got a legacy COM dll that I'd like to use in my multithreaded C# app. Problem though is when I create several instances of the interop object and start them on their own threads they always seem to run synchronously. I've boiled down the code to a simple "lab" experiment below. In this console app you'll see that instance one comes back after roughly 4 seconds and then instance two comes back in 8 seconds, twice the...
1
3404
by: Marwan | last post by:
Hello I am using asynchronous delegates to make a call to a COM ActiveX object, but even though the call occurs on a separate thread, my UI is still blocking. If i put the thread to sleep in my delegate call, the application is well behaved (no UI freeze), but the call to the com object causes the UI to lock up Do I have to manage calls to an ActiveX object differently than using the BeginInvoke and a callback A sample of the code I...
3
3142
by: Ed | last post by:
Here's my situation, I am moving some of my apps over from native C++ (primarily Win32 and MFC). One of these apps uses some COM objects in worker threads running in the background where it doesn't bother the user. I'm keeping the native code that interfaces with the COM objects in a DLL since I know it works with no problem (I've used for a year and half with no bugs).
11
4272
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
9
2465
by: tommy | last post by:
hi, i have found a example for multithreading and asp.net http://www.fawcette.com/vsm/2002_11/magazine/features/chester/ i want to speed up my website ... if my website is starting, they should build a database-connection and send a few sqls
2
3741
by: shonend | last post by:
**** sorry about the length of the message. If you can't read the whole thing and still willing to help, read the last 2 paragraphs where the main problem is described. The introduction story is mentioned to, as much clear as possible, give a picture in what environment the problems rise**** Hello experts! I would appreciate if you can address this problem I have and give a hint what could be wrong.
5
5705
by: Yeti | last post by:
Hey everyone, I am modifying code upgraded from VB 6 to 2005. I need to run the 2005 code in the MTA mode so I set a sub main procedure in my module which runs first when the program starts: Public theApplicationForm As New Form1 <MTAThread()> Public Sub main() Application.Run(theApplicationForm) End Sub
4
2672
by: boo73uk | last post by:
Hi All, I'm going to rewrite a VB6 app to VB.net and I need some pointers. Basically this app spawns simultaneous,multiple, independant ActiveX.exe 'workers' which query a SQL Server database and BCP a load of data out. Now, what is the best way to approach this in .Net? Can .Net give me a better way of utilising the resources of the server? We're also getting some new hardware, 2 CPU Xeon rig with HT (hyper threading). Are there any...
2
2268
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I divided the complete drawing into 3 parts..1st will be done by main thread and other two are done in these procedures - <1LongTimeTask <2LongTimeTask2 I have invoked the threads using below method. **************
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9997
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5435
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.