Solved

Communication between MFC ocx and a thread

Posted on 2004-09-28
9
482 Views
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.
jlsjls
0
Comment
Question by:jlsjls
[X]
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
  • 4
  • 4
9 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
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).
0
 
LVL 3

Author Comment

by:jlsjls
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.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 12169823
You could use logic like
PostMessage(....)
WaitForEvent(...)
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:jlsjls
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...
0
 
LVL 44

Expert Comment

by:AndyAinscow
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).
0
 
LVL 3

Author Comment

by:jlsjls
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 :
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 );
}

0
 
LVL 3

Author Comment

by:jlsjls
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.
0
 
LVL 44

Expert Comment

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

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.

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

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 173
lucky13 challenge 11 167
Annoying "thing" blocks my view 4 146
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 1,690
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

751 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