By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,630 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,630 IT Pros & Developers. It's quick & easy.

ActiveX Control Running on Separate Thread

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.