Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to put a single monitor to sleep using WinAPI/C#?

By using the Windows API it is possible to put the monitors into sleep mode:

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

and then

SendMessage(this.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)2);

When running the command above, both monitors go to sleep. Is it possible to make this affect only one of multiple connected monitors?

like image 257
Erlend D. Avatar asked Dec 14 '10 19:12

Erlend D.


People also ask

How to use the sleep () function in Linux and Windows?

When we use the Linux or UNIX operating system, we need to include “unistd.h” header file in our program to use the sleep () function. While using the Windows operating system, we have to include “Windows.h” header to use the sleep () function.

How to put a program to sleep in C++?

The time provided is mostly in milliseconds, microseconds or seconds and depending on that we have various functions that can put the program to sleep. C++ language does not provide a sleep function of its own.

What is the difference between sleep () and usleep () functions in C++?

Sleep () and usleep () functions are same except for the time unit used to specify the sleep time. In sleep () function, the time is specified in seconds, while in usleep () function, the time is specified in microseconds. The thread functions sleep_for () suspends the thread execution for a specific time period provided as an argument.

What is the header file for sleep in C++?

Q #2) What is the header file for sleep in C++? Answer: The header for sleep is “unistd.h” for LINUX/UNIX Operating system and “Windows.h” for the Windows Operating system. For thread sleep that uses ‘sleep_for’ and ‘sleep_until’ functions, <thread> header is used.


2 Answers

Exactly I don't know if you can put into sleep a specific monitor.. but you can achieve this by changing number of displays to One (assuming you have 2), which you want to be awake. The other one will automatically goes to sleep after about a minute (most monitors enter power save mode after about a minute after disconnect). You are essentially disconnecting the monitor, programatically. You can activate it when you need it.

However this method will have other implications, which you will know once you start playing with it.

like image 196
Bhuvan Avatar answered Sep 22 '22 15:09

Bhuvan


According to this...no.

The reason being is that the API is turning off the display which is defined as both monitors. Turning off a specific monitor would be a hardware tie in.

like image 29
Aaron McIver Avatar answered Sep 21 '22 15:09

Aaron McIver