在DLL中封裝了一個非模態對話框窗口,但是在運行時希望能阻塞主線程,但是不阻塞界面
void DoEvent(); DWORD WINAPI doModal(LPVOID p) { INT i = 0; while (1) { i++; Sleep(30); if (i == 83)break; static MSG msg; if (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { AfxGetApp()->PumpMessage(); } } return 0; } void DoEvent(CScale3DCMessageBoxDlg &dlg) { MSG msg; //取消息,檢索應用程序的消息隊列,PM_REMOVE取過之後從消息隊列中移除 if (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { //發消息 ::TranslateMessage(&msg); ::DispatchMessage(&msg); dlg.SendMessage(msg.message, msg.wParam, msg.lParam); } } extern "C" __declspec(dllexport) VOID __stdcall CreateMsgBox(CWnd * parent) { CScale3DCMessageBoxDlg pDlgMsgBox; pDlgMsgBox.Create(IDD_DIALOG1, parent); pDlgMsgBox.ShowWindow(SW_SHOW); HANDLE hThread[1]; DWORD threadId; hThread[0] = CreateThread(NULL, 0, doModal, 0, 0, &threadId); DWORD dwRet; do { dwRet = ::MsgWaitForMultipleObjects(1, hThread, FALSE, INFINITE, QS_ALLINPUT); if (dwRet != WAIT_OBJECT_0) { pDlgMsgBox.UpdateWindow(); DoEvent(pDlgMsgBox); } } while ((dwRet != WAIT_OBJECT_0) && (dwRet != WAIT_FAILED)); }