多线程程序设计时,程序退出时的处理方式

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

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

cpp文件:

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);
}
}

return 0;
}

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);
}
}

return 0;
}

初始化:

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);

你可能感兴趣的:(多线程,退出时的处理方式)