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

  1. UINT CMainUIThreadWaitDlg::StartThread(LPVOID pParam)  
  2. {  
  3.     ((CMainUIThreadWaitDlg*)pParam)->ThreadWork();  
  4.     return 0;  
  5. }  
  6.   
  7. void CMainUIThreadWaitDlg::ThreadWork()  
  8. {  
  9.     OutputDebugString(_T("+++++Start+++++\r\n"));  
  10.     Sleep(20*1000);  
  11.     OutputDebugString(_T("++++++End+++++\r\n"));  
  12. }  
  13.   
  14. void CMainUIThreadWaitDlg::DoEvent()  
  15. {  
  16.     MSG msg;  
  17.     if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE))  //取消息,检索应用程序的消息队列,PM_REMOVE取过之后从消息队列中移除  
  18.     {  
  19.         //发消息  
  20.         ::TranslateMessage(&msg);   
  21.         ::DispatchMessage(&msg);  
  22.     }  
  23. }  
  24.   
  25. CWinThread* pThread = NULL;  
  26.   
  27. void CMainUIThreadWaitDlg::OnBnClickedBtnWork()  
  28. {  
  29.     m_btnWork.EnableWindow(FALSE);      //按钮禁灰  
  30.     pThread = AfxBeginThread(StartThread,this);  //起线程  
  31.     DWORD dwRet;  
  32.     DoEvent();  
  33.     do   
  34.     {  
  35.         dwRet = ::MsgWaitForMultipleObjects(1, &pThread->m_hThread, FALSE, INFINITE, QS_ALLINPUT);  
  36.         if (dwRet != WAIT_OBJECT_0)  
  37.         {  
  38.             DoEvent();  
  39.         }  
  40.     } while ((dwRet != WAIT_OBJECT_0) && (dwRet != WAIT_FAILED));  
  41.     m_btnWork.EnableWindow(TRUE);       //按钮点亮  
  42. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值