Link to home
Start Free TrialLog in
Avatar of jlsjls
jlsjls

asked on

Communication between MFC ocx and a thread

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.
jlsjls
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

'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).
Avatar of jlsjls
jlsjls

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jlsjls

ASKER

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...
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).
Avatar of jlsjls

ASKER

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 :
WNDCLASSEX WndClass =
{
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();
}
else
{
::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)
{
case WM_CREATE:
      pThis = (CBanksysCtrl*)((LPCREATESTRUCT) lParam)->lpCreateParams;
      ::SetWindowLong( hwnd, 0, (LONG) pThis );
      return 0;

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

Avatar of jlsjls

ASKER

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.
Your welcome.  Glad you could get it sorted out.