Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static int SetWaitForWakeUpTime(DateTime wakeTime) {
- long waketime = wakeTime.ToFileTime();
- String timerName = "MyWakeUpTimer";
- 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;
- }
- Thread t = new Thread((o) => {
- WakeData wd2 = (WakeData) o;
- try {
- DateTime now = DateTime.Now;
- DateTime next = now.Date.AddTicks(wd2.wakeTime.TimeOfDay.Ticks);
- if (next < now)
- next = next.AddDays(1);
- wd2.result = SetWaitForWakeUpTime(next);
- } catch (Exception ex) {
- wd2.ex = ex;
- }
- });
- t.IsBackground = true;
- WakeData wd = new WakeData { wakeTime = wakeTime.Value };
- t.Start(wd);
- private class WakeData {
- public DateTime wakeTime;
- public int result;
- public Exception ex;
- }
- [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);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement