2
0

WindowsPowerManagement.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5. using MediaBrowser.Controller.Power;
  6. using Microsoft.Win32.SafeHandles;
  7. namespace MediaBrowser.ServerApplication.Native
  8. {
  9. public class WindowsPowerManagement : IPowerManagement
  10. {
  11. [DllImport("kernel32.dll")]
  12. public static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, string lpTimerName);
  13. [DllImport("kernel32.dll", SetLastError = true)]
  14. [return: MarshalAs(UnmanagedType.Bool)]
  15. public static extern bool SetWaitableTimer(SafeWaitHandle hTimer, [In] ref long pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, bool fResume);
  16. public void ScheduleWake(DateTime utcTime)
  17. {
  18. long duetime = utcTime.ToFileTime();
  19. using (SafeWaitHandle handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer"))
  20. {
  21. if (SetWaitableTimer(handle, ref duetime, 0, IntPtr.Zero, IntPtr.Zero, true))
  22. {
  23. using (EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset))
  24. {
  25. wh.SafeWaitHandle = handle;
  26. wh.WaitOne();
  27. }
  28. }
  29. else
  30. {
  31. throw new Win32Exception(Marshal.GetLastWin32Error());
  32. }
  33. }
  34. }
  35. }
  36. }