线程跨类无法关闭的问题???
MainFrame类里
CWinThread* pThread = NULL;//全局变量
void CMainFrame::OnManageUser()//工具栏的按钮按下消息,弹出对话框并触发一个线程。
{
if(!ShowObj.ShowWindow(SW_SHOW))
{
pThread = AfxBeginThread((AFX_THREADPROC)MyThreadProc,GetSafeHwnd());
// pThreadOut = pThread;
// bflagTime = TRUE;
}
}
UINT WINAPI MyThreadProc(LPVOID pParam)
{
while(1);
}
//对话框类里关闭消息关闭线程,关闭不了。卡住并强制关闭程序。。。。
extern CWinThread* pThread;
void AlarmDlg::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
DWORD dwECode;
GetExitCodeThread(pThread->m_hThread,&dwECode);
if(dwECode == STILL_ACTIVE)
{
TerminateThread(pThread->m_hThread,0);
CloseHandle(pThread->m_hThread);
pThread->m_hThread = NULL;
}
CDialog::OnClose();
}
::AfxEndThread(0);//随便问下,这种关闭线程的方法怎么连个线程句柄都不需要指定呢????
------解决方案-------------------- DWORD dwECode; GetExitCodeThread(pThread->m_hThread,&dwECode); if(dwECode == STILL_ACTIVE) { TerminateThread(pThread->m_hThread,0); CloseHandle(pThread->m_hThread); pThread->m_hThread = NULL; }
这个也是错误的, 你需要反复的while循环,判断线程是否活着
while(1)
{
getexitcode
if(返回码==still_active)
{
关闭线程
}
else
break;
}
因为 你执行关闭线程函数后,不是说立即就关闭了。
一家之言,不对的,请之处,并附代码验证