我正试图让我的电脑唤醒,以防它进入睡眠模式。我在某个网站上找到了这个代码片段,但我最后添加的 Messagebox 总是立即返回。
我还在电源选项中启用了我的系统以使用唤醒计时器。
[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 IntPtr handle;
private void SetWaitForWakeUpTime()
{
long duetime = 1200000000;
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 !");
}
我这样称呼它:
private void buttonTest_Click(object sender, EventArgs e)
{
SetWaitForWakeUpTime();
}
从理论上讲,这应该只在 2 分钟后发出信号,对吧?为什么它会立即发出信号,我该如何纠正?