Threads and windowless ActiveX controls
-
In a method of an OCX, I start a thread. I want to fire an event of the control at the end of the thread. How can I start this event in the thread of the OCX? I cannot do a PostMessage because the control is not a window. It seems to be an overkill to create a hidden window that would allow me to do so... Any idea?
-
In a method of an OCX, I start a thread. I want to fire an event of the control at the end of the thread. How can I start this event in the thread of the OCX? I cannot do a PostMessage because the control is not a window. It seems to be an overkill to create a hidden window that would allow me to do so... Any idea?
I suspect this is a question to be answered in the COM forum. I think you should be able to marshal the pointer to the container's callback interface across to your worker thread, and then call it from there. COM should have created a proxy object for the thread which can perform the appropriate marshalling to get back onto the UI thread. Look up
CoMarshalInterThreadInterfaceInStream
and the counterpart,CoGetInterfaceAndReleaseStream
. Note that I'm only guessing - I've never done this. -
In a method of an OCX, I start a thread. I want to fire an event of the control at the end of the thread. How can I start this event in the thread of the OCX? I cannot do a PostMessage because the control is not a window. It seems to be an overkill to create a hidden window that would allow me to do so... Any idea?
. I want to fire an event of the control at the end of the thread If you really want to fire an event at the end of the thread, then it would be easier to just save args of your events and fire them on your main thread after secondary thread has finished. Alternatively, of course you can use interface pointer marshaling and or GIT, however you will be faced then again with troubles of thread sync, because your windowless activeX doesn't have it's own message pump. Therefore, I would suggest going with the hidden window, and I don't think it's overkill compared with interface pointer marshaling... "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me
-
In a method of an OCX, I start a thread. I want to fire an event of the control at the end of the thread. How can I start this event in the thread of the OCX? I cannot do a PostMessage because the control is not a window. It seems to be an overkill to create a hidden window that would allow me to do so... Any idea?
If you wanna fire an event at the end of your worker thread then the best thing is to monitor your thread execution status with GetExitCodeThread and fire the event directly from your control class. In order to do this you have to set the thread's autodelete member to FALSE. This should do the job:
CWinThread* pThread = AfxBeginThread(...,CREATE_SUSPENDED);
pThread->m_bAutoDelete = FALSE;
pThread->ResumeThread();DWORD dwExitCode;
while (TRUE)
{
GetExitCodeThread(pThread->m_hThread, dwExitCode);
if (dwExitCode != STILL_ACTIVE)
{
FireYourEvent();
break;
}
else
SleepABitAndPumpMessages();
}Peter Molnar