How to turn on monitor after wake-up from suspend mode? [closed]
Asked Answered
S

2

2

I need to wake up a PC from sleep to perform some actions using C#.

I've used CreateWaitableTimer functions, everything goes fine. At given time the PC wakes up but the monitor stays in power save mode (turned off)!

So I want to know, how to turn the monitor ON after wake up?

PS I've tried "Complete Guide on How To Turn A Monitor On/Off/Standby" - with SendMessage (Codeproject) and SetThreadExecutionState(ES_DISPLAY_REQUIRED) - it doesn't work for me.

Any ideas?

Stramonium answered 5/4, 2010 at 9:40 Comment(1)
This question make zero sense. A monitor is for a human to look at, it is not a flashlight.Eringo
P
0

Try making the mouse move. When i wake up my Windows 7 system with a tap on the keyboard the screen stays black until i move the mouse.

Cursor.Position = new Point(x, y);
Paulettapaulette answered 5/4, 2010 at 9:47 Comment(2)
No, it also doesn't work. But after i turn on monitor by pressing keyboard key - i've noticed that cursor set to (x,y) position, so code been executedStramonium
You can programatically cause a mouse move that will wake the monitor by using the user32.dll function mouse_event. See this answer https://mcmap.net/q/300793/-turn-on-off-monitor to see how to do it in code; and see the 4th comment down to see how to do it in a .bat file, or powershell or Task Scheduler.Delila
B
0

for me it works fine to use pinvoke to call SendMessage.
code example for csharp:

using System;
using System.Runtime.InteropServices;

namespace MyDummyNamespace
{
   class MyProgram
   {
      private static int Main(string[] args)
      {
         // your program code here
         // ...

         NativeMethods.MonitorOff();
         System.Threading.Thread.Sleep(5000);
         NativeMethods.MonitorOn();

         return 0;
      }

      private static class NativeMethods
      {
         internal static void MonitorOn()
         {
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (IntPtr)MONITOR_ON);
         }

         internal static void MonitorOff()
         {
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (IntPtr)MONITOR_OFF);
         }

         [DllImport("user32.dll", CharSet = CharSet.Auto)]
         private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

         private static int MONITOR_ON = -1;
         private static int MONITOR_OFF = 2;
         private static int MONITOR_STANBY = 1;

         private static IntPtr HWND_BROADCAST = new IntPtr(0xffff);
         private static UInt32 WM_SYSCOMMAND = 0x0112;
         private static IntPtr SC_MONITORPOWER = new IntPtr(0xF170);
      }
   }
}

the above solution was inspired by this answer: https://mcmap.net/q/303925/-how-do-i-wake-up-my-windows-monitors-once-turned-off-by-power-settings

Bracknell answered 22/2, 2017 at 13:55 Comment(1)
FYI this method to turn off the screen is a hack - you are trying to broadcast a message you don't control to all windows on the desktop? Microsoft explicitly doesn't support this behavior. You should never send messages that are explicitly documented as being messages sent only by the system. This may not work on future versions of Windows and you definitely shouldn't be broadcasting to all windows. Please avoid using this technique if possible.Gaiser

© 2022 - 2024 — McMap. All rights reserved.