• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Multithreading in an ActiveX control

I have a implemented a worker thread spawned from an MFC control for use in VB.  The thread executes fine but when I attempt to fire an event in the control (passing a string as a parameter) from the thread VB dies with a memory exception error as soon as it attempts to read the event parameter.  How does one fire events in controls safely accross thread boundaries?

The documentation mentions CoMarshalInterThreadInterfaceInStream for this sort of thing (?) but this requires an interface pointer.  If this is the right approach, how do I get this pointer given that it is a windowless control (ie CWnd::GetControlUnknown returns NULL)
1 Solution
Does the code tat fires the event run on the same thread as code of normal methods  of your ActiveX control executes (== is your event fired from within a method of the control or from within a complete different thread) ?

Does your control support Apartment Model Threading?  If it does, then you can only access the control from one thread: the thread that created it.  You can find out more about Apartment Model Threading in VC++ online help: "Apartment-Model Threading in ActiveX Controls" (TN064).

To make you program work, your worker thread has to notify the main thread when it needs to fire an event or call a method in the control.  You main thread has to detect the notification and do the work (access the control).

If you use a user interface thread instead of a worker thread, it will be a lot easier.  You can send messages between threads (see online help on the CWinThread class and the PostThreadMessage function).

amornementAuthor Commented:
At the risk of sounding stupid, how do I notify a control that doesn't have a window handle associated with it to fire an event/method?  How do I get the control to respond to the notification?

Also, as I mentioned, CoMarshalInterThreadInterfaceInStream seems to be the starting point for doing this sort of thing automatically without having to post messages all over the place.  Where do I get the parameters to pass to it?  At the moment it returns INVALID_PARAM or something along those lines.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now