问题描述
我需要从睡眠中唤醒PC才能使用C#执行某些操作.
I need to wake up a PC from sleep to perform some actions using C#.
我使用了CreateWaitableTimer
函数,一切正常.在给定的时间,PC会唤醒,但显示器仍处于省电模式(关闭)!
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我已经尝试过关于如何打开/关闭/待机显示器的完整指南"-使用SendMessage(代码项目)和SetThreadExecutionState(ES_DISPLAY_REQUIRED)-对我不起作用.
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.
有什么想法吗?
推荐答案
对我来说,使用pinvoke调用SendMessage
效果很好.
csharp的代码示例:
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);
}
}
}
上述解决方案的灵感来自以下答案: https://stackoverflow.com/a/332733/1468842
the above solution was inspired by this answer: https://stackoverflow.com/a/332733/1468842
这篇关于从挂起模式唤醒后如何打开显示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!