G
G
Glace2021-04-09 01:53:34
C++ / C#
Glace, 2021-04-09 01:53:34

How to reduce CPU usage when the program window is minimized?

I am writing a WinApi program. Creating a program window with a message handler is all like in the manuals. ALL interaction with the program occurs through a single on_message() function which is called from the WndProc() callback function. While the program is not minimized, the processor load is at an acceptable level (~ 5% and also the video card ~ 5% because DirectX set it to redraw), but when the window is minimized, the processor load increases sharply (~ 30%, although it should have dropped to zero ). At first I sinned on the message handler, put logs, but apart from messages for redrawing in minimized mode, almost nothing comes there (the rest of the messages are one or two times). Rigidly cut off message processing for redrawing with return, but nothing has changed. Searched the internet and found nothing. Tell me please,

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Glace, 2021-04-09
@Glace

Surprising as it may not be, the minimized program is indeed choked with WM_PAINT messages . Apparently, in the collapsed state, much more messages arrive in the program for redrawing than in the unfolded state (my assumption, which I don’t even want to check). I tried to prevent window redrawing by calling in case WM_SIZE: { if (wParam == SIZE_MINIMIZED) ... if (wParam == SIZE_RESTORED) ... } functions SendMessage(hWnd, WM_SETREDRAW, (WPARAM)false, NULL) and LockWindowUpdate(hWnd ) , but the first one gave a very strange result (the icon on the taskbar and not only disappeared when sending a message), the second one did not give any result at all. As a result, globally declared bool variable lock_draw = falseand set it to case WM_SIZE . The program leaned on a crutch and went. The code began to look something like this:

bool lock_draw = false;
bool Core::on_message(UINT message, WPARAM wParam, LPARAM lParam)
{
    if (lock_draw && message == WM_PAINT) return false;
    ...
    switch(message) {
        ...
        case WM_SIZE: 
            if (wParam == SIZE_MINIMIZED) lock_draw = true;
            if (wParam == SIZE_RESTORED) lock_draw = false;
        ...
    }
}
ps Asking the question to cut off the rendering messages, I tried the IsIconic(hWnd) function , which, judging by the profiler, takes no less CPU time than the o_O rendering function.

A
Alexander Ananiev, 2021-04-09
@SaNNy32

Run the profiler and see which function eats up CPU time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question