多線程程序設計時,程序退出時的處理方式

CWinThread* pTESTThread;
BOOL bRun;
BOOL bIsTesting;
BOOL bThreadExit;函數

BOOL TESTProc1();
CWinThread* pTESTThread1;
BOOL bRun1;
BOOL bIsTesting1;
BOOL bThreadExit1;測試

cpp文件:this

UINT TESTThread( LPVOID pParam )
{
CTestDlg *pTESTdlg = (CTestDlg *)pParam;進程

while(pTESTdlg->bThreadExit == FALSE)
{
Sleep(50);
if(pTESTdlg->bRun)
{
pTESTdlg->bIsTesting = TRUE;
if(pTESTdlg->TESTProc() == FALSE)
{
AfxMessageBox("生成報告出錯,請關閉打開的測試報告!");
}
pTESTdlg->bRun = FALSE;
::SendMessage(pTESTdlg->m_hWnd, WM_TESTDLG_UPDATEDATA, (WPARAM)FALSE, NULL); //進程函數中不能直接調用updatedata函數
//AfxMessageBox("測量過程中止,從新測量請按Run!");
}
else
{
pTESTdlg->bIsTesting = FALSE;
Sleep(20);
}
}it

return 0;
}date

UINT TESTThread1( LPVOID pParam )
{
CTestDlg *pTESTdlg = (CTestDlg *)pParam;文件

while(pTESTdlg->bThreadExit1 == FALSE)
{
Sleep(20);
if(pTESTdlg->bRun1)
{
pTESTdlg->bIsTesting1 = TRUE;
if(pTESTdlg->TESTProc1() == FALSE)
{
AfxMessageBox("fail");
}
pTESTdlg->bRun1 = FALSE;
::SendMessage(pTESTdlg->m_hWnd, WM_TESTDLG_UPDATEDATA, (WPARAM)FALSE, NULL); //進程函數中不能直接調用updatedata函數
//AfxMessageBox("測量過程中止,從新測量請按Run!");
}
else
{
pTESTdlg->bIsTesting1 = FALSE;
Sleep(20);
}
}while

return 0;
}生成

初始化:data

bRun = FALSE;
bIsTesting = FALSE;
bThreadExit = FALSE;
pTESTThread = AfxBeginThread(TESTThread, this);

bRun1 = FALSE;
bIsTesting1 = FALSE;
bThreadExit1 = FALSE;
pTESTThread1 = AfxBeginThread(TESTThread1, this);

退出操做:

bThreadExit = TRUE;
::WaitForSingleObject((HANDLE)pTESTThread->m_hThread, INFINITE);

bThreadExit1 = TRUE; ::WaitForSingleObject((HANDLE)pTESTThread1->m_hThread, INFINITE);

相關文章
相關標籤/搜索