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

消息放线程里发送就断言异常

2014-01-03 
消息放线程里发送就断言错误?一个SDI程序,有几个窗口,运行时从Doc打开若干文件,打开之后通过这些文件里的

消息放线程里发送就断言错误?
一个SDI程序,有几个窗口,运行时从Doc打开若干文件,打开之后通过这些文件里的信息向各个子窗口发送消息。问题如下:
发送消息如果放在主线程里,运行没有问题,就是会稍微“卡”一下,经过查找,发现是发送消息的时候“卡”了,现在想新开一个线程,发送消息,但是运行时触发断言错误,提示要发送消息的子窗口m_hwnd非法 。具体代码:

正常主线程里时


BOOL CCtrlSystemDoc::OnOpenDocument(LPCTSTR lpszPathName)     
 {      
         ...打开文件操作...
        //流速面板初始化
pMain->SendMessageToPane(0,WM_USER_MSG_INITPANE);
//频率面板初始化
pMain->SendMessageToPane(2,WM_USER_MSG_INITPANE);
//状态栏初始化
pMain->SendMessageToPane(3,WM_USER_MSG_INITPANE);
//控制进度条
pMain->SendMessageToPane(3,WM_USER_MSG_UPDATERTPROGRESSBAR);
}

以上运行没错
放到线程里

UINT InitPaneThread(LPVOID pParam)
{
CMainFrame* pMain = (CMainFrame*)AfxGetMainWnd();
ASSERT(pMain);

//流速面板初始化
pMain->SendMessageToPane(0,WM_USER_MSG_INITPANE);
//频率面板初始化
pMain->SendMessageToPane(2,WM_USER_MSG_INITPANE);
//状态栏初始化
pMain->SendMessageToPane(3,WM_USER_MSG_INITPANE);
//控制进度条
pMain->SendMessageToPane(3,WM_USER_MSG_UPDATERTPROGRESSBAR);

return(0);

}

就会触发断言错误 提示的是我的一个子窗口m_hwnd非法 
SendMessageToPane函数 

void CMainFrame::SendMessageToPane(int nPaneIndex,UINT msg)
{
if (nPaneIndex == 0)
{//流速pane

       m_VolecityPane.PostMessage(msg,NULL,NULL);
}
else if (nPaneIndex == 1)
{//输出pane
m_OutputPane.PostMessage(msg,NULL,NULL);
}
else if (nPaneIndex == 2)
{//频率pane
m_FreqPane.PostMessage(msg,NULL,NULL);
}
else if (nPaneIndex == 3)
{//状态栏
m_wndStatusBar.PostMessage(msg,NULL,NULL);
}

}




[解决办法]
If AfxGetMainWnd is called from the application's primary thread, it returns the application's main window according to the above rules. If the function is called from a secondary thread in the application, the function returns the main window associated with the thread that made the call.
把主窗口的Hwnd记录下来传到线程里
[解决办法]
猜测,线程运行时,子窗口还没绑定完成。

热点排行