- 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)) //取消息,检索应用程序的消息队列,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); //按钮点亮
- }
MFC 主界面函数中线程等待避免界面卡死的处理方法
最新推荐文章于 2024-07-05 08:35:31 发布