您的位置:首页 > 其它

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

2007-10-11 11:17 295 查看

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, 0, 0, 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐