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

qnetg123 2012-03-08 04:57:08
如题

http://topic.csdn.net/u/20120307/12/149122ec-9374-4ac4-bd70-2c6ac10ec1a3.html
解决后,又出来的问题
...全文
395 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qnetg123 2012-03-09
  • 打赏
  • 举报
回复
我自己顶下,静等高手帮助
赵4老师 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 qnetg123 的回复:]
最后解决掉了,
解决方法是
我加了一个全局变量Bool,
在向Socket服务器发送消息地方改变这个Bool 为false
在PostThreadMessage 地方改变这个Bool 为true
在GetMessage 地方,每次取到消息,查看这个Bool值,如果为true则退出
这样 ,不管消息是否会不会被GetMessage到,我都可以正确的得到返回
[/Quote]
条条大路通罗马。
qnetg123 2012-03-09
  • 打赏
  • 举报
回复
最后解决掉了,
解决方法是
我加了一个全局变量Bool,
在向Socket服务器发送消息地方改变这个Bool 为false
在PostThreadMessage 地方改变这个Bool 为true
在GetMessage 地方,每次取到消息,查看这个Bool值,如果为true则退出
这样 ,不管消息是否会不会被GetMessage到,我都可以正确的得到返回
rolandoo 2012-03-09
  • 打赏
  • 举报
回复

//Header File
#define QUIT_MSG_HANDLER WM_USER+0x001
#define MSG_HANDLER_TIMEOUT 3000
bool CreateMsgHandler();
void CloseMsgHandler();
friend unsigned int __stdcall ThreadMsgHandler(void *pMainFrame);
HANDLE m_hMsgHandlerEvent;
HANDLE m_hMsgHandler;
UINT m_uiMsgHandlerID;

//Source File
bool 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;
}
qnetg123 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 rolandoo 的回复:]
http://www.wretch.cc/blog/kuolun1003/13891010

參考看看 不知道對你有沒有幫助
[/Quote]

这个网址我打不陈
qnetg123 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhao4zhong1 的回复:]
被父窗口处理或过滤掉了?
[/Quote]
应该不会吧,,
话说消息ID 我是设9001 这个消息ID不会有重复的了吧
rolandoo 2012-03-08
  • 打赏
  • 举报
回复
http://www.wretch.cc/blog/kuolun1003/13891010

參考看看 不知道對你有沒有幫助
赵4老师 2012-03-08
  • 打赏
  • 举报
回复
被父窗口处理或过滤掉了?
qnetg123 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bluewanderer 的回复:]
在发送线程PostMessage之前,接收线程必须已经有消息队列了。消息队列只能通过GetMessage和PeekMessage建立。一般处理方法是进入消息循环之前调用一次PeekMessage,然后用不论任何手段通知发送线程消息队列已经建立,然后再PostMessage。
[/Quote]
你说的有道理,我应该也要这么加句
但是我的问题好像并不是这样子的,因为并不是第一次消息没了,而是中间有时候消息会没有收到。
bluewanderer 2012-03-08
  • 打赏
  • 举报
回复
在发送线程PostMessage之前,接收线程必须已经有消息队列了。消息队列只能通过GetMessage和PeekMessage建立。一般处理方法是进入消息循环之前调用一次PeekMessage,然后用不论任何手段通知发送线程消息队列已经建立,然后再PostMessage。
qnetg123 2012-03-08
  • 打赏
  • 举报
回复
我是查看了,PostThreadMessage 有发送了消息ID,并且返回也是true ,但是GetMessage就是没有收到,为什么呢

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧