What is the correct way to Schedule the display to sleep and wake it up afterwards?

Vishal2 Bansal 285 Reputation points
2025-11-03T06:56:08.79+00:00

Hi

let suppose today is 31 November 2025

I am scheduling a windows to sleep today at 10:00 pm and it will wake up at 10:00 am tomorrow.

This behavior I want to repeat every day except on some custom dates (3 November(holiday)) and days (all Sundays).

  • Case1: If on Saturday 10pm device turned off. It will wake up at 10:00 am Monday.
  • Case2: if on Monday it is holiday then it will wake up at 10:00 am Tuesday.

So now the issue is device wakeup worked fine (from wed-saturday) but on Sunday I found it woke up at 7:35 pm. Why so?

It should have wakeup on Tuesday 10:00 am (4 November 2025) (due to holiday on Monday and bypassing Sunday).

But it did not work. What can be the reasons?

Below is the code to create the wakeup timer.

Here dt is the date time at which it should wake up.

 public static void SetWakeAt(DateTime dt)
 {
     long interval = dt.ToFileTime();
     IntPtr timer = CreateWaitableTimer(IntPtr.Zero, true, "WakeTimer");
     if (timer == IntPtr.Zero)
     {
         throw new Exception("Unable to create waitable timer.");
     }
     if (!SetWaitableTimer(timer, ref interval, 0, IntPtr.Zero, IntPtr.Zero, true))
     {
         throw new Exception("Unable to set waitable timer.");
     }
   
 }

Below are the event viewers logs. See at 2025-11-02 07:35: Pm it wakes-up. Why??:-

User's image

Pls help to resolve it.

Windows development | Windows API - Win32
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.