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

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 CMyAXControlCtrl::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(this.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.Threading.ThreadStateException' 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(this.MyThreadTask));
threadAX.Start();
}

Thanks in advance,

Javier Bertran
Nov 16 '05 #1
2 5821
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/cpovrAsynchronousDelegates.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*************@TK2MSFTNGP12.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 CMyAXControlCtrl::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(this.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.Threading.ThreadStateException' 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(this.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
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...
2
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...
5
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...
5
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....
3
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...
0
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...
23
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...
6
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...
6
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.