473,657 Members | 2,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ActiveX Control Running on Separate Thread

Hi all,
I have an ActiveX control developed in Visual C++ 6.0 that I want to use in
a C# project. I want the ActiveX code to run on a separate thread, but can't
seem to get it to work. If for example my ActiveX member has an infinite
loop, the .NET app gives all processing to the ActiveX and never returns,
not even to redraw the window. Here is what I tried (in a nutshell):

VC++ 6.0 OCX code:

void CMyAXControlCtr l::Loop()
{
while(1) {} // infinite loop
}

C# application code:

private void MyThreadTask()
{
axMyAXControl1. Loop(); // calls the ActiveX infinite loop
function
}

private void foo()
{
threadAX = new Thread(new ThreadStart(thi s.MyThreadTask) );
threadAX.Start( );
}

Note that my Main function in C# is declared with [STAThread], if I declare
it as [MTAThread] I get an unhandled exception of type
'System.Threadi ng.ThreadStateE xception' in system.windows. forms.dll (Could
not instantiate ActiveX control because the current thread is not in a
single-threaded apartment.)

Of course if I do not use an AX control and have the equivalent code in my
C# project, the threading works fine (just made sure!!!):

private void MyThreadTask()
{
// axMyAXControl1. Loop(); // calls the ActiveX infinite loop
function
while (true) {}
}

private void foo()
{
threadAX = new Thread(new ThreadStart(thi s.MyThreadTask) );
threadAX.Start( );
}

Thanks in advance,

Javier Bertran
Nov 16 '05 #1
2 5859
hi Javier

One thing you can try instantiate an Asynchronous delegate then make it
point to your function , then use its begin invoke and end invoke instead
of creating your own thread .
You can just wrap your ActiveX function in a c# function and then
instantiate the delegate on this function .
Here you are a link to read more about that

http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpovrAsynchrono usDelegates.asp
hope this helps

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #2
First, all ActiveX controls need an STA thread to run on, so you need to set
the ApartmentState of the thread to STA before starting the thread.
Second, you need to pump the message loop in your newly created thread,
failing to do so can result in a deadlock.
Third, you should never loop infinitely as this will hog the CPU and disturb
the message pump.

Willy.

"Javier Bertran" <ja****@bertran .com> wrote in message
news:eC******** *****@TK2MSFTNG P12.phx.gbl...
Hi all,
I have an ActiveX control developed in Visual C++ 6.0 that I want to use
in a C# project. I want the ActiveX code to run on a separate thread, but
can't seem to get it to work. If for example my ActiveX member has an
infinite loop, the .NET app gives all processing to the ActiveX and never
returns, not even to redraw the window. Here is what I tried (in a
nutshell):

VC++ 6.0 OCX code:

void CMyAXControlCtr l::Loop()
{
while(1) {} // infinite loop
}

C# application code:

private void MyThreadTask()
{
axMyAXControl1. Loop(); // calls the ActiveX infinite loop
function
}

private void foo()
{
threadAX = new Thread(new ThreadStart(thi s.MyThreadTask) );
threadAX.Start( );
}

Note that my Main function in C# is declared with [STAThread], if I
declare it as [MTAThread] I get an unhandled exception of type
'System.Threadi ng.ThreadStateE xception' in system.windows. forms.dll (Could
not instantiate ActiveX control because the current thread is not in a
single-threaded apartment.)

Of course if I do not use an AX control and have the equivalent code in my
C# project, the threading works fine (just made sure!!!):

private void MyThreadTask()
{
// axMyAXControl1. Loop(); // calls the ActiveX infinite loop
function
while (true) {}
}

private void foo()
{
threadAX = new Thread(new ThreadStart(thi s.MyThreadTask) );
threadAX.Start( );
}

Thanks in advance,

Javier Bertran

Nov 16 '05 #3

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

Similar topics

1
3391
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...
2
4658
by: Martin Baker | last post by:
I have an activeX control which I am calling from a C# program, this works fine. I now want to call the activeX control from a seperate thread, this works but is very slow. This is far too slow to work properly, can anyone suggest how I can fix this? The program is not doing anything else while the thread is running, the idea is that the thread will be running continously in a loop to animate the activeX control. However it runs slow...
5
2462
by: Gaby | last post by:
Hi, I try to create a windows from with an ocx on it. When I call the form after a click event of a button it works fine. But when I try want to create the form based on some information from a MSMQ I get the message similar as the subject of this message. I've already changed my app to Single Thread :
5
2628
by: Dan | last post by:
Good Day All, I am having a problem in Visual Studio 2005 Beta 2. I am hoping someone might have an idea as to what is going on. I have an ActiveX User Control written using Visual Basic 6.0. Using Visual Studio 2005 I add the control to my Toolbox. I then drag the control onto a Panel control that is on a form. That is it. I don't write any code at all. When I run the program I get the following error:
3
2319
by: fumihiko | last post by:
Hi, I created an activex control (C++), and it uses another COM dll (C++). This COM dll links with a static library that dose some calculation. (both are debug multithreaded dll) I created a C# client having activex control embedded at the very beginning (added in the design window); then, I created another same activex control at the runtime. After the 2nd activex control is created, I start to get errors on the function calls to the...
0
1070
by: Dave Taylor | last post by:
I have an ActiveX control on a Windows form that provides OPC client functionality. The control has no visible elements. I would like to write a class which uses the control to pass data to an OPC server based on a timer...say every 5 seconds write data to the server. I know normally, I'd use the Invoke member to access control properties from a separate thread. However, if no other thread is accessing the control and there are no...
23
3454
by: Galen Somerville | last post by:
A VB6 ActiveX.exe raises an event which is seen by the VB6 App. Same setup in VB2005. The event to be raised is in form frmSweep. As in VB6, frmSweep is hidden when the events take place. I know by actions on the screen and sounds in the speakers that VB2005 is using the ActiveX.exe correctly. However the events are never raised !!!
6
1696
by: fantum | last post by:
I have created a small activex dll to do a background task on my web server. I fire it off with a set myjob = server.createobject(myactivex) and it runs. I do not do a set myjob = nothing and so it seems to keep working even after the initiating session is closed with session.abandon. Question... what will be the lifetime of this object and is there a way to tell if it is dead and needs to be reset... after the session is abandoned I no...
6
8043
by: hufaunder | last post by:
I have an ActiveX component that I want to use in a library that I am writing. As a first test I used the ActiveX component in a windows form application. Adding the component created: Ax.dll .dll I can not call the functions in the ActiveX component. In the next step I tried to use the ActiveX component in a class library. I simply added a reference to the corresponding COM component. This this only
0
8827
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...
1
8504
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
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...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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
4159
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
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
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
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.