I've created a thread that auto-saves some text in a control every 30 seconds. However, it's not very good code and I've never done threading before. Currently, the code in the threads Run() function looks something like this... Code: while(!m_bKill) { while(!m_bRunning) { } CSingleLock lock(&m_mMutex, TRUE); if(lock.IsLocked()) { if(m_pNote) { m_pNote->PostMessage(WM_AUTOSAVE); } else { m_bRunning = FALSE; lock.Unlock(); } while(m_bRunning) { } lock.Unlock(); } else { m_bRunning = FALSE; } } return 0; There is a timer elsewhere that flips m_bRunning to TRUE causing the WM_AUTOSAVE message to be sent. The function that handles that sets m_bRunning back to FALSE. This is a pretty ugly setup and the while-loops for m_bRunning are clearly not ideal as they just sit and spin on the CPU. How else could I execute this code and then put the thread to sleep for a defined period of time before executing again? I've read in numerous places that Sleep() is not a very good thing to do in UI threads for some reason... Thanks for your help
I've since changed the code to the following... Code: while(!m_bKill) { Sleep(30000); // sleep for ~ 30 seconds m_bRunning = TRUE; CSingleLock lock(&m_mMutex, TRUE); if(lock.IsLocked()) { if(m_pNote) { m_pNote->PostMessage(WM_AUTOSAVE); } while(m_bRunning) { Sleep(1000); } lock.Unlock(); } else { m_bRunning = FALSE; } } return 0; So no more timer and the thread utilizes Sleep(). Anyone have any thoughts? :|