Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Multithreading in an ActiveX control

Posted on 1999-08-03
Medium Priority
Last Modified: 2013-11-20
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)
Question by:amornement
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1312468
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) ?


Accepted Solution

Liu earned 400 total points
ID: 1312469
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).


Author Comment

ID: 1312470
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.


Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question