我MFC 主界面函数中线程等待避免界面卡死的处理方法

2025-10-08 09:32:00

实用场景:

例如在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)) //取消息,检索应用程序的消息队列,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); //按钮点亮

}