I am running a GUI app on windows device. The main thread for this is the UI thread. Im adding a functionality to turn off the display when the application is not being used for certain time.
I tried using the following API's to turn off the display:
HWND hwnd = GetForegroundWindow();
PostMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
or SendMessagewith the same parameters. And im triggerring a mouse event to turn on the display again:
mouse_event(1, 40, 0, 0, 0);
Functionality wise, everything is working fine. But when the device is transitioning to display off state, my main thread is getting stalled for 700 to 800 ms.
Is this because my gui app'ss renderring is affected during display state transition or what other possibilities could be causing this issue.
The main thread is active again after 800 ms.
1 Answer 1
I'm not sure what you're specifically trying to do by sending SC_MONITORPOWER to the foreground window, but this can cause unexpected behavior in some cases.
If the foreground window is a special window, a child window, or a window of a process that overrides WM_SYSCOMMAND, blocks or doesn't handle SC_MONITORPOWER, the monitor may not turn off correctly or will turn back on shortly after.
If you want to be sure the monitor turns off correctly, use this instead:
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
Update: Sorry for the misunderstanding, now I see your actual problem. As IInspectable said in his comment above, having a thread suspended for an indefinite period of time is entirely common in a preemptive multitasking system, specificialy on Windows, it's not your application's issue. But you can prevent it from happening by using SendMessageTimeout or sending SC_MONITORPOWER from a subthread.
GetForegroundWindow(), please use your own window. Then please do not use tags that are irrelevant to the question.gpudirectX