Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a question regarding message windows. I have a class (CWnd based), in which I create a message only window. Then I add the
C#
BEGIN_MESSAGE_MAP(MyClass, CWnd)
    //{{AFX_MSG_MAP(MyClass)
    ON_REGISTERED_MESSAGE(WM_MyClass_MSG_TO_THREAD,OnMyClassMsg2)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()


then in a working thread I create a that class
MyClass *class = new MyClass;
Now in the UI thread I find the window
::FindWindow(...);
and then post a message to it
::PostMessage(...);
I use user registered messages, I do find the window, but the message is never captured by the window, so I don't get anything in the OnMyClassMsg2 function.
Is there anything wrong I'm doing?
Posted

OK, so you alocated and created a window in a worker thread.

So the next question, is what is the worker thread doing? (Did it already finish, or is it busy doing some other work?)

A worker thread doesn't natively have a message loop, so unless you are explicitly getting messages and dispatching them somewhere in your worker thread, then you'll never see the message.

Typically you'd do something like:

while ( im_not_doing_anything_else ) 
{
    MSG m;
    GetMessage(&m,0,0,0);
    TranslateMessage(&m);
    DispatchMessage(&m);
}
 
Share this answer
 
After you allocate the class you need to call create() on it:

MyClass *class =  new MyClass;
MyClass->create();


See: http://msdn.microsoft.com/en-us/library/0yhc9kx4(v=VS.90).aspx[^]
 
Share this answer
 
I was creating the window in the class itself
C++
void MyClass::MyClass(void){
        CString wnd_class_name = ::AfxRegisterWndClass(NULL);
	this->CreateEx(0,wnd_class_name, "TEST_THREAD_WINDOW",0 ,0 ,0 ,0 ,0 ,HWND_MESSAGE,0);
	TRACE("WINDOW READY\n");
}


I did what you suggested and the result was the same, as I said, I can find the window, the FindWindowEx(..) returns HWND, so the window is there, but the message is simply not captured by it. I have double checked the registers message and so on and everything is ok, but no result in the function for the received message.

Edit: I mean FindWindowEx, not FindWindow of course.
 
Share this answer
 
v2
Comments
TRK3 1-Aug-11 14:04pm    
If you add a comment to my solution then I'll get an email letting me know you commented. Adding a new solution doesn't notify me.

(Also, since it isn't a solution, but a further explanation of the question, it shouldn't be a solution. Either edit your original question to add the additional data, or comment on the proposed solution.)

That said. See my additional solution below.
Thanks, that did the trick, I didn't know that the working thread doesn't have a message loop. Thanks, Solved!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900