// sets the wakeup timers and suspends the computer
        public void SetupSuspendForDuration(SuspendPowerState powerState, DateTime wakeTime, DateTime? nextWakeTime)
        {
            LogMessage("SystemServices", "SetupSuspendForDuration: " + wakeTime, LogEntryType.Trace);

            SetupWakeEvent(wakeTime, nextWakeTime);

            if (powerState == SuspendPowerState.Idle)
                return;

            // Allow a little time to ensure timer is ready
            Thread.Sleep(1000);

            RestartOptions ro = RestartOptions.Suspend;

            if (powerState == SuspendPowerState.Hibernate)
                ro = RestartOptions.Hibernate;
            WindowsController.ExitWindows(ro, false);
        }
        private void RequestSuspend(DateTime wakeTime, DateTime? nextWakeTime, SuspendPowerState suspendType)
        {
            DateTime now = DateTime.Now;
            TimeSpan duration = ((DateTime)wakeTime) - now;

            // Following sleep added to that computer does not suspend immediately after PVBC service startup
            // Waits 20 seconds - just enough to change my monitor settings after starting the service
            if (duration > TimeSpan.FromMinutes(2.0))
            {
                Thread.Sleep(20000);
                now = DateTime.Now;
                duration = ((DateTime)wakeTime) - now;
            }

            if (suspendType == SuspendPowerState.Hibernate)
            {
                if (duration < TimeSpan.FromSeconds(180))
                    duration = TimeSpan.FromSeconds(180);  // avoid timer expiring before suspend complete, hibernate needs longer time
            }
            else if (duration < TimeSpan.FromSeconds(60))
                duration = TimeSpan.FromSeconds(60);

            GlobalSettings.SystemServices.LogMessage("RequestSuspend", "Suspending for " + duration.TotalMinutes + " minutes", LogEntryType.Information);

            GlobalSettings.SystemServices.SetupSuspendForDuration(suspendType, now + duration, nextWakeTime);
        }