Never use
Sleep()
in the main (GUI) thread.
As already mentioned there are multiple resources about threads.
An example is
Using Worker Threads[
^].
Based on that article you should create a worker thread and an event to stop the thread. Then the worker thread function can look like:
UINT CMyThreadClass::ThreadFunc(LPVOID p)
{
CMyThreadClass * self = (CMyThreadClass *)p;
bool bStop = false;
while (!bStop)
{
switch (::WaitForSingleObject(self->m_StopEventHandle, 50))
{
case WAIT_OBJECT_0 :
bStop = true;
break;
case WAIT_TIMEOUT :
break;
}
}
return 0;
}
While this provides a better timing accuracy than a normal timer, it will still not be absolute accurate. If you use for example blocking socket calls in the worker thread, it might be necessary to adjust the time out value accordingly by measuring the execution time.