我的应用程序(我正在处理的安装程序的引导应用程序需要启动一些其他应用程序(我的安装程序和第三方安装程序,用于我的安装程序的先决条件)并等待它们完成。为了允许 GUI 进行屏幕更新在等待应用程序完成时,我使用 Visual Studio 文档中关于空闲循环处理的“MFC 兼容”示例作为指导,在等待循环中放置了一个消息泵。我的代码(位于 CWinApp 的成员函数中-派生类)如下:
if (::CreateProcess(lpAppName, szCmdLineBuffer, NULL, NULL, TRUE, 0, NULL, NULL,
&StartupInfo, &ProcessInfo))
{
::GetExitCodeProcess(ProcessInfo.hProcess, &dwExitCode);
if (bWait)
while (dwExitCode == STILL_ACTIVE)
{
// In order to allow updates of the GUI to happen while we're waiting for
// the application to finish, we must run a mini message pump here to
// allow messages to go through and get processed. This message pump
// performs much like MFC's main message pump found in CWinThread::Run().
MSG msg;
while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!PumpMessage())
{
// a termination message (e.g. WM_DESTROY)
// was processed, so we need to stop waiting
dwExitCode = ERROR_CANT_WAIT;
::PostQuitMessage(0);
break;
}
}
// let MFC do its idle processing
LONG nIdle = 0;
while (OnIdle(nIdle++))
;
if (dwExitCode == STILL_ACTIVE) // was a termination message processed?
{
// no; wait for .1 second to see if the application is finished
::WaitForSingleObject(ProcessInfo.hProcess, 100);
::GetExitCodeProcess(ProcessInfo.hProcess, &dwExitCode);
}
}
::CloseHandle(ProcessInfo.hProcess);
::CloseHandle(ProcessInfo.hThread);
}
else
dwExitCode = ::GetLastError();
我遇到的问题是,在某些时候,此消息泵似乎释放了我在运行此代码时打开的窗口上的窗口和菜单句柄。我在调试器中进行了遍历,它从来没有进入 if (!PumpMessage()) 语句的主体,所以我不知道这里发生了什么导致窗口和菜单句柄消失南。如果我没有消息泵,一切正常,只是在等待循环运行时 GUI 无法自行更新。
有没有人对如何使这项工作有任何想法?或者,如果 bWait 为 TRUE,我想启动一个工作线程来启动第二个应用程序,但我以前从未对线程做过任何事情,所以我需要一些关于如何在不引入同步问题等的情况下做到这一点的建议.(无论哪种情况,代码示例都将不胜感激。)