AfxThreading messed up.
I have an app that uses CWinThread / AfxThread functions.
Basically it is a file caching algorithm that needs the ability to
terminate immediately at anytime. Right now I have to basically have
many checks to see if a boolean is false and then exit and the class
deconstructor waits for the thread to exit naturally.
Caching Algorithm
MAIN THREAD (DEFAULT PRIORITY)
|----> SPAWN Caching Thread (LOWEST PRIORITY)
|------> SPAWNS File Copy Thread (LOW PRIORITY)
Deconstructor needs to close file copy and the caching thread
immediately. All threads have accessible handles already.
Some simple example code, probably ignore the file copy stuff level,
would be greatly appreciated.
SAMPLE CODE OF WHAT I HAVE ALREADY
==================================
void MVFileManager::disableCaching()
{
_caching_enabled = false;
if(_cache_monitor_thread != NULL)
{
/*
_cache_monitor_thread->ResumeThread();
// We have to wait until the caching is over.
while(_filecopyjobs.GetCount() > 0)
{
}
*/
unsigned long dwExitCode;
_cache_monitor_thread->SuspendThread();
GetExitCodeThread(_cache_monitor_thread->m_hThread, &dwExitCode);
TerminateThread(_cache_monitor_thread->m_hThread, dwExitCode);
CloseHandle(_cache_monitor_thread->m_hThread);
MVCacheJob* cjob;
while(_filecopyjobs.GetCount() > 0)
{
((MVCacheJob*)_filecopyjobs[0])->Thread->SuspendThread();
cjob = (MVCacheJob*) _filecopyjobs[0];
GetExitCodeThread(cjob->Thread->m_hThread, &dwExitCode);
TerminateThread(cjob->Thread->m_hThread, dwExitCode);
CloseHandle(cjob->Thread->m_hThread);
_filecopyjobs.RemoveAt(0);
}
}
_cache_monitor_thread = NULL;
}
void MVFileManager::enableCaching()
{
return;
disableCaching();
_caching_enabled = true;
CWinThread *pThread = AfxBeginThread(Thread_CacheMonitor, this,
THREAD_PRIORITY_LOWEST, 0, CREATE_SUSPENDED);
pThread->m_bAutoDelete = true;
_cache_monitor_thread = pThread;
pThread->ResumeThread();
}
=========================================================