第二回showwindow失败,无效的窗口句柄

第二次showwindow失败,无效的窗口句柄


UINT CInteractSelect::ShowSelect(int nDiag,DWORD dwTimeOut,const string &strSelect)
{
tracelog(trace_debug,"CInteractSelect::ShowSelect  dwTimeOut = [%d] strSelect = [%s]",dwTimeOut,strSelect.c_str());
l_nSelect = -1;
l_showSelect = strSelect;

HWND hWnd = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_SELECT),NULL,DialogProc);
if(!IsWindow(hWnd))
{
tracelog(trace_debug,"CreateDialog Error  :[%d]",GetLastError());
}
else
{
tracelog(trace_debug,"CreateDialog Success  :[%d]",hWnd);
}
BOOL ret;
ret = ShowWindow(hWnd,SW_SHOW);
ret = UpdateWindow(hWnd);
if(!ret)
{
tracelog(trace_debug,"UpdateWindow error:[%d]",GetLastError());
}
MSG msg;
if(dwTimeOut != 0)
{
SetTimer(hWnd,10010,dwTimeOut,NULL);
}

while(ret = GetMessage(&msg,NULL,0,0)) {
if(ret == -1)
{
tracelog(trace_debug,"CInteractSelect::ShowSelect GetMessage Failed  :[%d]",GetLastError());
}
TranslateMessage(&msg);
DispatchMessage(&msg);
    }
tracelog(trace_debug,"CInteractSelect::ShowSelect GetMessage Ended with  :[%d],Error Code:[%d]",ret,GetLastError());
DestroyWindow(hWnd);
return l_nSelect+1;
}




该段代码第一次运行的时候一切正常,第二次运行的时候窗口一闪而过。
第二次运行时CreateDialog函数创建窗口正常返回,IsWindow判断也正常,但是showwindow的时候却提示无效的窗口句柄,运行到这一句的时候窗口一闪而过,而下面的GetMessage函数却执行正常,请问该问题如何解决?
窗口 对话框 消息循环 win32

------解决方案--------------------
估计消息队列中留了WM_QUIT消息 
先把消息队列清空试试看


MSG pMsg = {0};
while(PeekMessage(&pMsg, NULL, NULL, NULL, PM_REMOVE));