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

∥☆過路亽.° 提交于 2019-11-28 02:03:08

问题


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?


回答1:


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://stackoverflow.com/a/332733/1468842




回答2:


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);


来源:https://stackoverflow.com/questions/2577720/how-to-turn-on-monitor-after-wake-up-from-suspend-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!