venAdder
May 19th, 2006, 06:58 PM
hi,
I ahve the follwing:
//system.h
class CSystem
{
...
static bool stopSim;
};
//system.cpp
bool CSystem::stopSim = false;
I start a new thread in a class with above variable as control variable as follows:
while( !CSystem::stopSim ){
...
}
FindClose( hFind );
dlg->m_btnSysStart.EnableWindow( TRUE );
dlg->m_btnSysStop.EnableWindow( FALSE );
CSystem::systemRunning = false;
return 0;
Now, when i exit app, i need to let this thread complete it's job and then exit gracefully so i do this
void CLaunchbar::OnBnClickedLbrexitapp()
{
// TODO: Add your control notification; handler code here
int ret = AfxMessageBox( "Are you sure you want to exit?", MB_YESNO );
if( ret == IDYES )
{
CSystem::stopSim = true;
//wait for the threads to exit
if( CSystem::systemRunning )
{
DWORD code = 0;
CMainFrame *parent = (CMainFrame*)this->GetParent( );
if( WaitForSingleObject(parent->m_dlgSys.m_handleSimThread,5000) == WAIT_TIMEOUT )
//dunno what's goign on here but ****er won't finish even when stop sim is set to true
TerminateThread( parent->m_dlgSys.m_handleSimThread, code );
}
theApp.Cleanup( );
AfxGetMainWnd()->PostMessage(WM_CLOSE);
}
}
now the problem is I set teh control variable to true and then wait for the thread to finish using WaitForSingle Object. But it always go to timeout( i tried infinite, it never finishes).
what am i doign wrong here?
I ahve the follwing:
//system.h
class CSystem
{
...
static bool stopSim;
};
//system.cpp
bool CSystem::stopSim = false;
I start a new thread in a class with above variable as control variable as follows:
while( !CSystem::stopSim ){
...
}
FindClose( hFind );
dlg->m_btnSysStart.EnableWindow( TRUE );
dlg->m_btnSysStop.EnableWindow( FALSE );
CSystem::systemRunning = false;
return 0;
Now, when i exit app, i need to let this thread complete it's job and then exit gracefully so i do this
void CLaunchbar::OnBnClickedLbrexitapp()
{
// TODO: Add your control notification; handler code here
int ret = AfxMessageBox( "Are you sure you want to exit?", MB_YESNO );
if( ret == IDYES )
{
CSystem::stopSim = true;
//wait for the threads to exit
if( CSystem::systemRunning )
{
DWORD code = 0;
CMainFrame *parent = (CMainFrame*)this->GetParent( );
if( WaitForSingleObject(parent->m_dlgSys.m_handleSimThread,5000) == WAIT_TIMEOUT )
//dunno what's goign on here but ****er won't finish even when stop sim is set to true
TerminateThread( parent->m_dlgSys.m_handleSimThread, code );
}
theApp.Cleanup( );
AfxGetMainWnd()->PostMessage(WM_CLOSE);
}
}
now the problem is I set teh control variable to true and then wait for the thread to finish using WaitForSingle Object. But it always go to timeout( i tried infinite, it never finishes).
what am i doign wrong here?