列出当前有效的CreateWaitableTimer事件

时间:2017-06-26 02:36:22

标签: c# pinvoke dllimport

尝试查找任何仍处于待处理状态的当前活动计时器,这些计时器可能会导致计算机被唤醒。创建计时器时,会指定名称。所有指定计时器的列表都是理想的,而不仅仅是指定的名称。

以下是创建命名计时器的代码:

[DllImport("kernel32.dll")]
private static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, String lpTimerName);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWaitableTimer(SafeWaitHandle hTimer, [In] ref long pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, bool fResume);

private static int SetWaitForWakeUpTime(DateTime wakeTime) {
    long waketime = wakeTime.ToFileTime();

    String timerName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString() + "WakeUpTimer";
    using (SafeWaitHandle handle = CreateWaitableTimer(IntPtr.Zero, true, timerName)) {
        if (SetWaitableTimer(handle, ref waketime, 0, IntPtr.Zero, IntPtr.Zero, true)) {
            using (EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset)) {
                wh.SafeWaitHandle = handle;
                wh.WaitOne();
            }
        }
        else {
            return Marshal.GetLastWin32Error();
        }
    }
    return 0;
}

我尝试使用NtQuerySystemInformation方法但没有成功。我甚至不确定该功能是否正确使用。

[DllImport("ntdll.dll", SetLastError=true)]
private static extern NtStatus NtQuerySystemInformation(uint infoClass, IntPtr info, uint size, out uint length);

我也尝试过使用命令行powercfg /waketimers,但这似乎只列出了Windows Task Scheduler选项的Wake the computer to run this task个任务。

0 个答案:

没有答案