关于网友提出的“ 子线程收不到主线程问题,求指教”问题疑问,本网通过在网上对“ 子线程收不到主线程问题,求指教”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 子线程收不到主线程问题,求指教
描述:null
通过在定时器内每隔一段时间就给子线程发送一个消息用于通知子线程该去工作了。
if (!PostThreadMessage(g_controlCenter.m_gMSProtocolAnalyes.m_dwThreadID, BT_DOWN_SEARCHE_CMD,0,0))
{
}
子线程中通过消息循环捕捉消息:
MSG msg;
PeekMessage(&msg,NULL,0,0,PM_REMOVE);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
int n = BT_DOWN_SEARCHE_CMD;
if (msg.message == BT_DOWN_SEARCHE_CMD)
{
pMSProtocol->SendDownTransData();
}
}
但是每次捕捉到的msg.message 都是0,是不是就相当于没有捕捉到消息啊?
解决方案1: PostThreadMessage会把你的消息放入消息队列中,不会立即被执行的
解决方案2: Because the return value can be nonzero, zero, or -1, avoid code like this:
while (GetMessage( lpMsg, hWnd, 0, 0)) ...
The possibility of a -1 return value means that such code can lead to fatal application errors. Instead, use code like this:
Hide Example
BOOL bRet;
while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
试试呢,应该能收着,挺普通的用法。
解决方案3: 参照物
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)
解决方案4:
peek的时候已经remove了,get的时候就没有了啊。不需要前面那句,直接get就行吧。
以上介绍了“ 子线程收不到主线程问题,求指教”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3290817.html