C# call to ActiveX(written by MFC)

L

linuxfedora

Hi All,

If i have a activeX which is written by MFC, and i put it into the C#
container(Program) and when i use the thread in the ThreadPool
(BackgroundWorker) to call the ActiveX function, it seems that the
called function will switched to another thread (Not the same thread
in my C#) program, anyway to prevent it? And what thread will be used
when called into the ActiveX function?Thanks.
 
P

Peter Duniho

linuxfedora said:
Hi All,

If i have a activeX which is written by MFC, and i put it into the C#
container(Program) and when i use the thread in the ThreadPool
(BackgroundWorker) to call the ActiveX function, it seems that the
called function will switched to another thread (Not the same thread
in my C#) program, anyway to prevent it? And what thread will be used
when called into the ActiveX function?Thanks.

An ActiveX object is a COM object and, as I learned recently, .NET's COM
interop will automatically marshal calls to an STA COM object to that
object's owning thread.

This has some implications:

-- The thread owning the ActiveX control must live at least as long
as the ActiveX control itself
-- The thread owning the ActiveX control must have a message pump
-- The ActiveX code will always be executed on the thread that owns
that control

The thread that owns the control is the thread where the object was
created. If you're using BackgroundWorker, then you probably have a
Forms or WPF application, where there's a main STA thread used for all
the UI code, and that thread is probably the one that owns your object.
Without an actual concise-but-complete code example from you however,
it would not be possible to tell you precisely which thread owns the object.

This all assumes the ActiveX control is an STA object. My recollection
is that they always are, but if I'm mistaken then one possible solution
would be to change your ActiveX control so that it's an MTA object and
then create it on a non-STA thread.

Otherwise, you will simply have to deal with the object's code always
executing on the STA thread that owns it.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top