Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. private static int SetWaitForWakeUpTime(DateTime wakeTime) {
  2. long waketime = wakeTime.ToFileTime();
  3.  
  4. String timerName = "MyWakeUpTimer";
  5. using (SafeWaitHandle handle = CreateWaitableTimer(IntPtr.Zero, true, timerName)) {
  6. if (SetWaitableTimer(handle, ref waketime, 0, IntPtr.Zero, IntPtr.Zero, true)) {
  7. using (EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset)) {
  8. wh.SafeWaitHandle = handle;
  9. wh.WaitOne();
  10. }
  11. }
  12. else {
  13. return Marshal.GetLastWin32Error();
  14. }
  15. }
  16. return 0;
  17. }
  18.  
  19. Thread t = new Thread((o) => {
  20. WakeData wd2 = (WakeData) o;
  21. try {
  22. DateTime now = DateTime.Now;
  23. DateTime next = now.Date.AddTicks(wd2.wakeTime.TimeOfDay.Ticks);
  24. if (next < now)
  25. next = next.AddDays(1);
  26.  
  27. wd2.result = SetWaitForWakeUpTime(next);
  28. } catch (Exception ex) {
  29. wd2.ex = ex;
  30. }
  31. });
  32. t.IsBackground = true;
  33. WakeData wd = new WakeData { wakeTime = wakeTime.Value };
  34. t.Start(wd);
  35.  
  36. private class WakeData {
  37. public DateTime wakeTime;
  38. public int result;
  39. public Exception ex;
  40. }
  41.  
  42. [DllImport("kernel32.dll")]
  43. private static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, String lpTimerName);
  44.  
  45. [DllImport("kernel32.dll", SetLastError = true)]
  46. [return: MarshalAs(UnmanagedType.Bool)]
  47. private static extern bool SetWaitableTimer(SafeWaitHandle hTimer, [In] ref long pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, bool fResume);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement