I am trying to create a WIN32 (C++) program in which I will have to simultaneously process messages and run a while loop. In order to do that, I want to use threads.
When I moved the message loop to a separate procedure (called from the function WinMain), all things were working fine. However, when I used the code below to thread that procedure, instead of simply calling it from the main process, the window becomes unresponsive.
Do you know why that happens?
Inside WinMain, after creating the main window, I removed the message loop and the return value, adding the following piece of code:
std::thread t1(message_loop);
t1.join();
return return_val;
return_val
is a global variable which I will use to receive the value WinMain should return when the message loop ends.
Also, the function message_loop is as follows:
void message_loop()
{
MSG messages;
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
return_val = messages.wParam;
}