CWinThread* m_pSendThread; // 初始化 m_pSendThread = NULL;
//启动
m_pSendThread = AfxBeginThread(SendFileThreadFunc,this, THREAD_PRIORITY_ABOVE_NORMAL, 0, CREATE_SUSPENDED);
m_pSendThread->m_bAutoDelete = FALSE;//手动删除
m_pSendThread->ResumeThread();
//关闭
if (m_pSendThread != NULL )
{
m_bIsSendFile = FALSE; //volatileBOOL m_bIsSendFile 线程函数中的一个中断标记
waiteMSGAndObject(m_pSendThread->m_hThread); //等待线程的结束
m_pSendThread->m_bAutoDelete = TRUE;
delete m_pSendThread;
m_pSendThread = NULL;
}
//MsgWaitForMultipleObjects
void waiteMSGAndObject( HANDLE handle )
{
DWORD dwRet = 0;
MSG msg;
while (TRUE)
{
//wait for m_hThread to be over,and wait for
//QS_ALLINPUT(Any message is in the queue)
dwRet = MsgWaitForMultipleObjects (1, &handle, FALSE, INFINITE, QS_ALLINPUT);
switch(dwRet)
{
case WAIT_OBJECT_0:
break; //break the loop
case WAIT_OBJECT_0 + 1:
//get the message from Queue
//and dispatch it to specific window
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
DispatchMessage(&msg);
continue;
default:
break; // unexpected failure
}
break;
}
}