Search code examples
windowswinapiwindow

When should I use a wait function like MsgWaitForMultipleObjects, and when shouldn't I?


So I am trying to understand the message processing code of Unreal Engine on Windows OS, and I didn't find any frequent usage of the function MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx in the message pumping code. The engine message pumping goes like this:

MSG Message;

    // standard Windows message handling
    while(PeekMessage(&Message, NULL, 0, 0, PM_REMOVE))
    { 
        TranslateMessage(&Message);
        DispatchMessage(&Message); 
    }

For context, this code will run every frame one to three times, meaning the code will be executed each 2 - 5 milliseconds on average throughout the running time of the application. A) Does that make wait functions unnecessary? or am I missing something here!

B) Is there any rough estimation of how long an application could be busy doing 'other stuff' before processing incoming messages? For instance if an application only processes messages every 50 millisecond, is that a bad practice? or is that a reasonable way of doing it? And what if the period became 500 milliseconds and so?


Solution

  • Use MsgWaitForMultipleObjects/etc if you need to both handle window message processing and kernel handle or alertable waits in a single thread. If you are only doing message processing then simply use a normal GetMessage based message loop, if only doing kernel handle or alertable waits then use WaitForMultipleObjects as appropriate.