我正在阅读其他人的代码并且他们的代码可以正常工作。我只是对他们使用的postmessage和消息循环感到有点困惑
#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);
}
}
代码看起来像这样,但稍作修改
根据 MSDN 当PostMessage的_In_opt_ HWND hWnd设置为null时
The function behaves like a call to PostThreadMessage with the dwThreadId parameter set to the identifier of the current thread.
这让我很困惑。看来主线程创建的t线程真的收到了msg?谁能解释一下?
顺便说一句,我是 mfc 的新手,这是我在这里的第一篇文章,如果我有任何误解,请告诉我
最佳答案
您似乎在调用 CWnd::PostMessage 函数,而不是 API PostMessage 函数。因此,该调用使用其 HWND 成员向 myClass 对话框发布一条消息,该消息将在进行调用的同一线程中接收。
关于c++ - 不同线程中的postmessage和message loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30746851/