首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

小弟我用PostThreadMessage 发送了消息,可是为什么有时候 GetMessage 收不到这个消息呢

2012-03-26 
我用PostThreadMessage 发送了消息,可是为什么有时候 GetMessage 收不到这个消息呢如题是http://topic.csd

我用PostThreadMessage 发送了消息,可是为什么有时候 GetMessage 收不到这个消息呢
如题

http://topic.csdn.net/u/20120307/12/149122ec-9374-4ac4-bd70-2c6ac10ec1a3.html
解决后,又出来的问题

[解决办法]
在发送线程PostMessage之前,接收线程必须已经有消息队列了。消息队列只能通过GetMessage和PeekMessage建立。一般处理方法是进入消息循环之前调用一次PeekMessage,然后用不论任何手段通知发送线程消息队列已经建立,然后再PostMessage。
[解决办法]
被父窗口处理或过滤掉了?
[解决办法]
http://www.wretch.cc/blog/kuolun1003/13891010

參考看看 不知道對你有沒有幫助
[解决办法]

C/C++ code
//Header File#define QUIT_MSG_HANDLER WM_USER+0x001#define MSG_HANDLER_TIMEOUT 3000bool CreateMsgHandler();void CloseMsgHandler();friend unsigned int __stdcall ThreadMsgHandler(void *pMainFrame);HANDLE m_hMsgHandlerEvent;HANDLE m_hMsgHandler;UINT m_uiMsgHandlerID;//Source Filebool CMainFrame::CreateMsgHandler(){    m_hMsgHandler = INVALID_HANDLE_VALUE;    m_hMsgHandlerEvent = INVALID_HANDLE_VALUE;    m_uiMsgHandlerID = 0;    m_hMsgHandlerEvent = CreateEvent(NULL, FALSE, FALSE, "Msg Handler Event");    if (m_hMsgHandlerEvent == NULL)    {        ::MessageBox(NULL,"Create msg handler event error","Warning",MB_OK);        return false;    }    //Create message handler thread    m_hMsgHandler = (HANDLE) _beginthreadex(NULL,1024,ThreadMsgHandler,        (void *) this, 0,(unsigned int *) &m_uiMsgHandlerID);    if (m_hMsgHandler == NULL)    {        ::MessageBox(NULL,"Create msg handler thread error","Warning",MB_OK);        return false;    }    switch (WaitForSingleObject(m_hMsgHandlerEvent, MSG_HANDLER_TIMEOUT))    {    case WAIT_OBJECT_0:        break;    case WAIT_TIMEOUT:    default:        ::MessageBox(NULL,"Msg handler event not signaled\n","Warning",MB_OK);        return false;    }    return true;}//By Reference http://blog.csdn.net/qingfeng_happy5/article/details/3515283//I change my close msg hander function...void CMainFrame::CloseMsgHandler(){    if (m_hMsgHandler != INVALID_HANDLE_VALUE)    {        PostThreadMessage(m_uiMsgHandlerID,QUIT_MSG_HANDLER,(WPARAM) NULL,(LPARAM) NULL);        DWORD dRet;        MSG msg;        while (1)        {            dRet=::MsgWaitForMultipleObjects(1,&m_hMsgHandler,FALSE,INFINITE,QS_ALLINPUT);            if (dRet == WAIT_OBJECT_0+1)            {                while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))                {                    TranslateMessage(&msg);                    DispatchMessage(&msg);                }            }            else            {                CloseHandle(m_hMsgHandler);                m_hMsgHandler = INVALID_HANDLE_VALUE;                CloseHandle(m_hMsgHandlerEvent);                m_hMsgHandlerEvent = INVALID_HANDLE_VALUE;                break;            }        }    }}unsigned int __stdcall ThreadMsgHandler(void * pMainFrame){    CMainFrame *pMainFrm = (CMainFrame*)pMainFrame;    MSG msg;    bool bQuit = false;    PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);    SetEvent(pMainFrm->m_hMsgHandlerEvent);    while(!bQuit)    {        GetMessage(&msg, NULL, NULL, NULL);        switch (msg.message)        {        case QUIT_MSG_HANDLER:            {                bQuit = true;                break;            }        }        Sleep(10);    }    return 0;} 

热点排行