I have an application which second thread calls GetMessage() in a loop. At some point the first thread realizes that the user wants to quit the application and notifies the second thread that it should terminate. As the second thread is stuck on GetMessage(), the program never quits. Is there a way to wait for messages with a timeout?
I’m open to other ideas too.
EDIT: (additional explanations)
The second thread runs that snippet of code:
while ( !m_quit && GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
The first thread sets m_quit to true.
The easiest way is to just call
UINT_PTR timerId=SetTimer(NULL, NULL, 1000, NULL)before callingGetMessage. It will post aWM_TIMERmessage to the calling thread every second, so theGetMessagewill return promptly. Then, callKillTimer(NULL, timerId)to cancel it.UPDATE Sample code: