从一个站点获得the below post。有人试过吗?
框架中没有任何内容,因此您必须稍微“PInvoke”。
您需要调用的 API 是 CreateWaitableTimer 和 SetWaitableTimer。
以下是一个完整的 (Q&D) 示例,说明了如何设置
使用上述 Win32 API 将系统从睡眠/休眠中唤醒。
请注意,这里我设置了 300000000 nSecs 的相对唤醒时间。
这意味着计算机将被唤醒(假设他睡着了或
休眠)在设置定时器后 30 秒内。
有关 SetWaitableTimer 及其参数的详细信息,请参阅 MSDN 文档。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Willys
{
class Program
{
[DllImport("kernel32.dll")]
public static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes,
bool bManualReset, string lpTimerName);
[DllImport("kernel32.dll")]
public static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long
pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr
lpArgToCompletionRoutine, bool fResume);
[DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
public static extern Int32 WaitForSingleObject(IntPtr handle, uint
milliseconds);
static void Main()
{
SetWaitForWakeUpTime();
}
static IntPtr handle;
static void SetWaitForWakeUpTime()
{
long duetime = -300000000; // negative value, so a RELATIVE due time
Console.WriteLine("{0:x}", duetime);
handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer");
SetWaitableTimer(handle, ref duetime, 0, IntPtr.Zero, IntPtr.Zero, true);
uint INFINITE = 0xFFFFFFFF;
int ret = WaitForSingleObject(handle, INFINITE);
MessageBox.Show("Wake up call");
}
}
}