Win32 getmessage loop stops executing when App window looses focus..! i want to run a infinite loop in my application without stopping

StackOverflow https://stackoverflow.com/questions/19320492

  •  30-06-2022
  •  | 
  •  

Question

Win32 getmessage() loop stops executing when App window looses focus..! i want to run a infinite loop in my application without stopping...

The program works flawless when the mouse pointer moves on the window,as it raises a event and the getmessage processes,but when the application window looses its focus or the mouse is not over it,it STOPS!...

i have a function which needs to be called continuously even when the win32 app is minimized or lost focus...

i am calling the function like this...

MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    showFrame();
    }

i know,only when any event is raised the getmessage processes it and in turn the showFrame() function is called,but i want to call the function continuously...

please give me a clue how can i achieve it or do i need to use threading??

Was it helpful?

Solution

GetMessage() blocks until there is a message available to retrieve. If you don't want to wait, then use PeekMessage() instead:

MSG msg;
do
{
    ...
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        ...
    }
    ...
}
while (...);

You can also use either GetQueueStatus() or MsgWaitForMultipleObjects() to detect when there are messages available before then retrieving them, eg:

MSG msg;
do
{
    ...
    if (GetQueueStatus(QS_ALLINPUT))
    {
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            ...
        }
    }
    ...
}
while (...);

MSG msg;
do
{
    ...
    if (MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_ALLINPUT) == WAIT_OBJECT_0)
    {
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            ...
        }
    }
    ...
}
while (...);

Alternatively, you can use SetTimer() to post periodic WM_TIMER messages to the queue, and then your loop can show the frames each time it receives that message.

Alternatively, use a standard GetMessage() loop, and have a separate thread drive your frame logic. Whenever it needs to show a new frame, it can post a user-defined message to the main thread queue so your loop can show the frame.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top