在MFC程序的主线程中如果使用WaitForSingleObject等线子线程,而子线程里使用了有关于消息的函数,比如SetWindowText,InsertItem,SetItemText这些函数,就有会导致主主线程阻塞问题,看看这段代码。
DWORD WINAPI ThreadProc(
LPVOID lpParameter // thread data
)
{
CMFCWaitThreadDlg *pDlg = (CMFCWaitThreadDlg*)lpParameter;
for (int i=0;i<=10;i++)
{
OutputDebugStringA("test");
Sleep(2000);
pDlg->SetWindowText(_T("HEHE"));
}
return true;
}
void CMFCWaitThreadDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
DWORD dwID;
HANDLE hThread;
hThread = CreateThread(0,0,ThreadProc,this,0,&dwID);
DWORD dwRet = WaitForSingleObject(hThread,INFINITE);
if (dwRet == WAIT_OBJECT_0)
{
MessageBoxA(0,"Thread exit",0,0);
}
}
主线程出现阻塞问题的原因是,因为SetWindowText调用了SendMessage,借助了消息循环,然后主线程又Waitxxxxx,必然会阻塞,最终导致工作线程Crash掉了。还好微软提供了另一个函数MsgWaitForMultipleObjects,用这个就能搞定了,看看下面这段代码,把WaitForSingleObject那一段给替换了,跑一跑试试,就没问题了。
DWORD WINAPI ThreadProc(
LPVOID lpParameter // thread data
)
{
CMFCWaitThreadDlg *pDlg = (CMFCWaitThreadDlg*)lpParameter;
for (int i=0;i<=10;i++)
{
OutputDebugStringA("test");
Sleep(2000);
pDlg->SetWindowText(_T("HEHE"));
}
return true;
}
void CMFCWaitThreadDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
DWORD dwID;
HANDLE hThread;
hThread = CreateThread(0,0,ThreadProc,this,0,&dwID);
while(TRUE)
{
DWORD result ;
MSG msg ;
result = MsgWaitForMultipleObjects(1, &hThread,
FALSE, INFINITE, QS_ALLINPUT);
if (result == (WAIT_OBJECT_0))
{
break;
}
else
{
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
DispatchMessage(&msg);
}
}
MessageBoxA(0,"Thread exit",0,0);
}