title: Using a Waitable Timer from within a Windows Service
body: <p>I am attempting to use a WaitableTimer in a Windows Service written in C++ to bring a Windows XP machine out of sleep/stand-by mode but I cannot seem to get it to work. I copy/pasted the code I am using in the service into a standalone app and that works fine. Is there a step I am missing to get this to work within a service? </p>
  
  <p>The code I am using to set up the waitable timer is as follows (the call to UpdateWaitableTimer() happens within a thread that loops indefinitely):</p>
  
  <pre><code>void UpdateWaitableTimer(job_definition *jobdef)
  {
      HANDLE existingHandle;
      try
      {
          if (jobdef-&gt;JobType == JOB_TYPE_SCAN)
          {
              char szTimerName[MAX_PATH];
              sprintf_s(szTimerName, MAX_PATH, "Timer_%I64d", jobdef-&gt;JobID);
  
              existingHandle = OpenWaitableTimer(TIMER_ALL_ACCESS, TRUE, szTimerName);
  
  
              if (existingHandle != NULL)
              {
  
                  // A timer handle already exists for this job, so cancel it
                  CancelWaitableTimer(existingHandle);
              }
              else
              {
  
                  // No timer handle exists, create one
                  existingHandle = CreateWaitableTimer(NULL, TRUE, szTimerName);
              }
  
              if (jobdef-&gt;JobStatus != JOB_STATUS_SCHEDULED)
              {
  
                  // This job was cancelled, so close the handle
                  CloseHandle(existingHandle);
              }
              else
              {
  
                  time_t now = time(NULL);
                  time_t dt = jobdef-&gt;JobScheduleTime;
  
                  while(dt &lt; now)
                  {
                      dt += 86400;
                  }
  
                  // Get a FILETIME one minute before
                  FILETIME utcFileTime = GetTimestamp(dt - 60);
  
                  // Convert to LARGE_INTEGER
                  LARGE_INTEGER dueTime;
                  dueTime.HighPart = utcFileTime.dwHighDateTime;
                  dueTime.LowPart = utcFileTime.dwLowDateTime;
  
                  SYSTEMTIME st;
                  FILETIME *ft = &amp;utcFileTime;
                  FileTimeToSystemTime(ft, &amp;st);
                  LogRelease(false, "Setting Timer for scheduled job: %02d/%02d/%d %02d:%02d:%02d", st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond);
  
  
                  if(SetWaitableTimer(existingHandle, &amp;dueTime, 0, NULL, NULL, TRUE))
                  {
                      if(GetLastError() == ERROR_NOT_SUPPORTED)
                      {
                          LogRelease(false, "Resume from sleep/stand-by feature not supported on this operating system.");
                      }
                  }
                  else
                  {
  
                      LogError(false, "Could not create timer. Error: %d", GetLastError());
                  }
              }
          }
      }
      catch(...)
      {
          LogError(false, "An exception occured while updating waitable timer for job %I64d", jobdef-&gt;JobID);
      }
  
      LogRelease(false, "Finished Updating Waitable Timer [Job:%I64d]", jobdef-&gt;JobID);
  }
  </code></pre>