主窗口退出时等待工作线程退出

CMyView::OnDestroy()
{
   m_pMyThread
->PostThreadMessage (WM_QUIT,0,0);//a work thread with UI pump 
   DWORD ret;
    
bool quit=false;
    
int quitwparam=0;
    
bool bThreadEnd = false;
    
do
   
{

        ret 
= ::MsgWaitForMultipleObjects(1&m_hThread, FALSE, 

            INFINITE, QS_ALLINPUT);

        
if (ret != WAIT_OBJECT_0)

        
{

            MSG msg;
            
while(::PeekMessage (&msg, NULL, 00, PM_NOREMOVE)) 
            
{
                
//do not feed WM_QUIT into the message pump!
                if(WM_QUIT==msg.message)
                
{
                    GetMessage(
&msg,0,0,0);//do not feed WM_QUIT into the message pump!
                    quit=true;
                    quitwparam
=(int)msg.wParam;
                }

                
else
                
{
                    VERIFY(AfxGetApp ()
->PumpMessage ());
                }

                
//if(WAIT_OBJECT_0 == WaitForSingleObject(m_hThread, 0))
               
// {
                    
//bThreadEnd = true;
                    
//break;
               
// }

            }


        }


    }
 while ((ret != WAIT_OBJECT_0) && (ret != WAIT_FAILED));
    
if(quit)
          PostQuitMessage(quitwparam);

    VERIFY(CloseHandle(m_hThread));
    m_hThread
=0;    

}

 

 

 

Refer to:

First Aid for the Thread-Impaired: Using Multiple Threads with MFC

More First Aid for the Thread Impaired: Cool Ways to Take Advantage of Multithreading

Waiting for a thread to terminate

你可能感兴趣的:(主窗口退出时等待工作线程退出)