I am not sure what you mean by:
"Mind you it's not synchronization problem since even I remove the synchronization the problem persists."
To answer your question your thread function does 2 things:
1. Sleep for 5 minutes.
2. Wait for mutex to be signaled.
While it is doing either of those it is unresponsive and nothing else will happen.
1. Take out the sleep
2. Make bParamRefreshRunning = false then signal the mutex when you want to shut down the thread. You may have to add a little more code to get around {do some important stuff}.
3. call _endthread() then wait on the thread handle to ensure thread has exited.
4. CloseHandle(threadhandle)
5. CloseHandle(mutex)
OR:
The best way to use WaitForSingleObject is to check the return value.
This way the timeout can be changed from INFINITE to some small value - some seconds to allow the thread function to fall through regularly.
unsigned __stdcall CMyApp::RefreshDBParams(LPVOID pParam)
{
((CMyApp *) AfxGetApp())->hParamsMutex = CreateMutex (NULL, FALSE, NULL);
TRY
{
while(bParamRefreshRunning)
{
DWORD retval = WaitForSingleObject(((CMyApp *) AfxGetApp())->hParamsMutex, 1000);
if(WAIT_OBJECT_0 == retval)
{
if(bParamRefreshRunning)
{ do some important stuff}
ReleaseMutex(THISAPP->hParamsMutex);
}
}
}
CATCH (CException, e)
{
THISAPP->LogError(e);
ReleaseMutex(((CMyApp *) AfxGetApp())->hParamsMutex);
return 0;
}
END_CATCH
return 0;
}