postmessage and message loop in different thread

726 views Asked by At

I am reading other people's code and their code is working. I just feel a little confuse about postmessage and the message loop they used

#define MESSAGE XXX //some number
BEGIN_MESSAGE_MAP(myClass, CDialog)
//...
ON_THREAD_MESSAGE(...)
END_MESSAGE_MAP()
... myClass::funcA(...)
{
    static HANDLE t = createThread(...., funcB,....)
    .....
    postmessage(MESSAGE)

}

... myClass::funcB(...)
{
    ....
    while(...)
    {
        TranslateMessage(&msg);
        dispatchMessage(&msg);
    }

}

the code looks like that but with a little modification

According to MSDN when _In_opt_ HWND hWnd of PostMessage is set to null

The function behaves like a call to PostThreadMessage with the dwThreadId parameter set to the identifier of the current thread.

this is confusing to me. It seems the t thread created by the main thread actually receive the msg? can anyone explain this?

btw, I am new to mfc and this is my first post here, if I misunderstand anything, let me know

1

There are 1 answers

2
ScottMcP-MVP On BEST ANSWER

You appear to be calling the CWnd::PostMessage function, not the API PostMessage function. So the call posts a message to the myClass dialog, using its HWND member, and the message will be received in the same thread that makes the call.