CPU usage is high since V9.5.0

Since upgrade from V9.1.0 to V9.5.0, we find that the CPU usage is always high in windows, not like before which is 0% in static state.

So, we checked the difference, and we found the Event Loop logic changed. In file Rendering/UI/vtkWin32RenderWindowInteractor.cxx Line386, the while loop is always running, after we insert a sleep in the while loop, the CPU usage is to 0%.

For now, we insert code like this:

while (!PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE))
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}

Before this, we tried to just add one line:

std::this_thread::sleep_for(std::chrono::milliseconds(1));

But, in some progress written by Qt, it is easy to no response, so we changed it.

Would anyone tell us the difference? and is there other way to decrease the CPU usage?

@mwestphal Your change to also look at inputs looks relevant? @jaswantp You changed timer handling?

I see a few culprits:

TBF the timer on Windows were basically reimplemented, so its not surprising that this passive behavior was not kept.

@xiaohuijieao feel free to open a PR to improve the current behavior if you want.

It uses busy-wait, so that is the reason for high cpu usage.

I hope this can be fixed quickly. Well behaved code should never busy wait.

the solution could be WaitMessage function (winuser.h) - Win32 apps | Microsoft Learn

guarded behind a !PeekMessage so that it goes into “sleep wait” when the message queue is empty i.e, vtk window not receiving any user input.

See https://gitlab.kitware.com/vtk/vtk/-/merge_requests/13097

In my local testing:

With my WaitMessage MR

CPU usage goes back to 0% when window loses focus

With v9.5.0

CPU usage does NOT go below 3.0%

It would be nice if anyone of you can independently verify my MR removes the busy wait.