0

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.

Jabberwocky
51.3k18 gold badges71 silver badges127 bronze badges
asked 2 days ago
8
  • What is the specific question? Commented yesterday
  • 2
    Do not use GetForegroundWindow(), please use your own window. Then please do not use tags that are irrelevant to the question.gpu directX Commented yesterday
  • 1
    Surely Windows itself contains settings to turn off the display on inactivity..? Why do you need more than that? Commented yesterday
  • So when the display is turned off upon your request, your program stalls for a couple of 100s milliseconds. Thats sounds pretty normal to me, that's how WIndows works. How oftwn do you turn the display of and on again? What is your actual problem ? This sounds like an XY Problem Commented yesterday
  • Do you want the monitor turned off or do you want the graphics data erased? Commented yesterday

1 Answer 1

-2

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.

answered yesterday
New contributor
Anthony Lee Stark is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

1 Comment

The foreground window is never a child window. Regardless, broadcasting the message instead merely accomplishes one thing: It sends the message many times to windows you do not control. It has the same issue you just identified, multiplied by the number of toplevel windows.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.