实用场景:
例如在MFC主界面某个Button Click事件中起一个线程去做处理一些事情,在起的线程运行完毕后,接着跑Click起线程后的代码,已达到按顺序执行,保证时许正确的目的。
问题:
通常处理一个线程等待用 WaitForSingleObject,这个放在主界面线程成中会造成主界面“卡死”,其原因是它将主界面的消息循环给阻塞了,即等不到线程结束的时候界面上的消息循环无法正常工作,从而造成界面“卡死”
处理办法:
在等待线程的同时,让消息循环一直工作
示例
- UINT CMainUIThreadWaitDlg::StartThread(LPVOID pParam)
- {
- ((CMainUIThreadWaitDlg*)pParam)->ThreadWork();
- return 0;
- }
-
- void CMainUIThreadWaitDlg::ThreadWork()
- {
- OutputDebugString(_T("+++++Start+++++\r\n"));
- Sleep(20*1000);
- OutputDebugString(_T("++++++End+++++\r\n"));
- }
-
- void CMainUIThreadWaitDlg::DoEvent()
- {
- MSG msg;
- if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE))
- {
-
- ::TranslateMessage(&msg);
- ::DispatchMessage(&msg);
- }
- }
-
- CWinThread* pThread = NULL;
-
- void CMainUIThreadWaitDlg::OnBnClickedBtnWork()
- {
- m_btnWork.EnableWindow(FALSE);
- pThread = AfxBeginThread(StartThread,this);
- DWORD dwRet;
- DoEvent();
- do
- {
- dwRet = ::MsgWaitForMultipleObjects(1, &pThread->m_hThread, FALSE, INFINITE, QS_ALLINPUT);
- if (dwRet != WAIT_OBJECT_0)
- {
- DoEvent();
- }
- } while ((dwRet != WAIT_OBJECT_0) && (dwRet != WAIT_FAILED));
- m_btnWork.EnableWindow(TRUE);
- }