Communication between MFC ocx and a thread

Posted on 2004-09-28
Medium Priority
Last Modified: 2013-11-20
Situation :
I've created an ocx in MFC. In one of its method, I need to create a thread.
The main thread of the ocx, continues processing after creating this working thread. This working thread receives during its creation a pointer to the ocx so it can use its public member functions.

Problem :
The main thread must be able to process messages send by the worker thread (now done by SendMessage). Once the main thread receives such a message, it'll call its private Event dispatching method.
I don't succeed in sending a message from the worker thread to the main thread.
The method 'SendMessage' hangs, I try to send a message to a hidden window created by the main thread.
How should this be handeld.
Thanks for any advice.
Question by:jlsjls
  • 4
  • 4
LVL 45

Expert Comment

ID: 12169391
'This working thread receives during its creation a pointer to the ocx so it can use its public member functions.'
That might not be a good idea.  Use messages to initiate processing and return results via event/memory protected by CCriticalSection or other mechanism.

'The main thread must be able to process messages send by the worker thread (now done by SendMessage). '
SendMessage could cause a blockage (each thread waiting for the other to respond before continuing), PostMessage would be more advisable.
Does your thread have a HWND for the OCX to pass the messages to.  (Using CWnd pointers and the like is dangerous between threads).

Author Comment

ID: 12169659
The reason why the worker thread uses 'SendMessage' is that it needs to wait till the ocx (application) has handled the event. The worker thread sends a message to a hidden window in the ocx. The window procedure should handle this message and fire an event to the application. The application needs to (based on parameters of the event) set some values in this event. When finished, the worker thread can process the results.

Sounds complicated, maybe I'm not at a good track.
LVL 45

Accepted Solution

AndyAinscow earned 1000 total points
ID: 12169823
You could use logic like
as you are firing an event back to signal the thread to continue.

Back to your original question.  
Are you passing the HWND of this hidden window to the thread.  pWnd->GetSafeHwnd() will get the HWND of a CWnd* object.  If you aren't using the HWND then that could be why the SendMessage isn't getting to the hidden window.
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.


Author Comment

ID: 12170269
Yes, I'm passing the HWND of the hidden window to the thread. I set it as a public member on my ocx and a pointer to this ocx is passed in the creation of the worker thread.
When debugging my worker thread, I can see the value of the HWND and it's the same as the one I noticed by using Spy++ (where I see the handle of the hidden window).
Maybe I don't need to create the hidden window, you refer to 'pWnd->GetSafeHwnd()' ; how can I retrieve that from an ocx.
I've created a hidden window by the method 'CreateWindow'. This returns a handle to a window which I'm using in my worker thread.
I need to leave the office now, so I won't immediately respond to your answer.
... I'll be back...
LVL 45

Expert Comment

ID: 12170329
pWnd->GetSafeHwnd() was just to give an example of getting a HWND from a CWnd pointer.

Lets concentrate on the message not getting to your OCX.
Can you put a messagebox into the message handler in your hidden window (that way we see if it is getting a message or not).

Author Comment

ID: 12170548
When I'm debugging, I don't receive the message in the message handler of the hidden window.
I've already added logging in this message handler.
I'm not sure if my message handler is correct?? Should I use some windows dispatching (thread with GetMessage() ;  DispatchMessage()?? How should this be done?

This is how I create the hidden window with its message handler :
sizeof(WNDCLASSEX),             // cbSize
0,                              // style
s_PPPWindowProc,                        // lpfnWndProc
0,                              // cbClsExtra
sizeof( void* ),                // cbWndExtra
AfxGetInstanceHandle(),                  // hInstance
NULL,                           // hIcon
NULL,                           // hCursor
NULL,                           // hbrBackground
NULL,                           // lpszMenuName
g_WindowName_PPPEvents,            // lpszClassName
NULL                            // hIconSm
ATOM Atom = RegisterClassEx( &WndClass );
// Create the hidden window
hWdnBanksys = NULL;
hWdnBanksys = CreateWindowEx(
0,                              // extended window style
g_WindowName_PPPEvents,     // pointer to registered class name
g_WindowName_PPPEvents,     // pointer to window name
0,                              // window style
0,                              // horizontal position of window
0,                              // vertical position of window
0,                              // window width
0,                              // window height
NULL,                           // handle to parent or owner window
0,                              // handle to menu, or child-window identifier
AfxGetInstanceHandle(),                  // handle to application instance
this );                         // pointer to window-creation data

if (hWdnBanksys == NULL)
 DWORD dwError = ::GetLastError();
::wsprintf(szPPPText,"Creation of hidden window succeeded.");

And window procedure (message handler) :
static LRESULT CALLBACK s_PPPWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
static CBanksysCtrl* pThis;
switch (uMsg)
      pThis = (CBanksysCtrl*)((LPCREATESTRUCT) lParam)->lpCreateParams;
      ::SetWindowLong( hwnd, 0, (LONG) pThis );
      return 0;

      //the worker thread uses WM_PPP_DISPLAYDATA in SendMessage to the hidden window
      return 0;
return ::DefWindowProc( hwnd, uMsg, wParam, lParam );


Author Comment

ID: 12564914
My error; the reason why the main thread wasn't responding is due to the fact that :
I've added the call WaitForSingleObject(...,INFINITE) and waited for an event which
wasn't fired by the worker thread.
AndyAinscow thanks anyhow for your effort to help me.
LVL 45

Expert Comment

ID: 12564964
Your welcome.  Glad you could get it sorted out.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…

607 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