MFC中如何通过点击主程序的stop按钮来终止子线程?
线程函数是:UINTThreadRun(LPVOIDpParam){………………………………while(1){………………………………}return0;}然后Run按钮运...
线程函数是:
UINT ThreadRun(LPVOID pParam)
{
………………………………
while(1)
{
………………………………
}
return 0;
}
然后Run按钮运行线程:
void CGetNetDlg::OnButtonRun()
{
………………………………
AfxBeginThread(ThreadRun,this);
………………………………
}
我想在按下stop后终止线程……Stop按钮的代码如何编写?
void CMyDlg::OnButtonStop()
{
// TODO: Add your control notification handler code here
} 展开
UINT ThreadRun(LPVOID pParam)
{
………………………………
while(1)
{
………………………………
}
return 0;
}
然后Run按钮运行线程:
void CGetNetDlg::OnButtonRun()
{
………………………………
AfxBeginThread(ThreadRun,this);
………………………………
}
我想在按下stop后终止线程……Stop按钮的代码如何编写?
void CMyDlg::OnButtonStop()
{
// TODO: Add your control notification handler code here
} 展开
展开全部
需要改动一下上面的程序,在调用AfxBeginThread时把此函数返回的CWinThread*变量保存起来,比如定义一个类成员变量CWinThread* m_pThread,调用AfxBeginThread的代码改成m_pThread= AfxBeginThread(ThreadRun,this);。而Stop里的代码可以写成:
DWORD dwCode;
GetExitCodeThread(m_pThread->m_hThread, &dwCode);
TerminateThread(m_pThread->m_hThread, dwCode);
CloseHandle(m_pThread->m_hThread);
DWORD dwCode;
GetExitCodeThread(m_pThread->m_hThread, &dwCode);
TerminateThread(m_pThread->m_hThread, dwCode);
CloseHandle(m_pThread->m_hThread);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询