private static void ThrowWhenFailed(NativeMethods.ExecutionStates previousState)
 {
     if (previousState == 0)
     {
         throw new Exception("Failed to suppress/restore system power management.");
     }
 }
        /// <summary>
        /// Prevents the system to enter any of the sleep states on idle.
        /// </summary>
        /// <remarks>
        /// This includes dimming and deactivation of display device. Does not affect activation of screen saver.
        /// </remarks>
        public static void Disable()
        {
            const NativeMethods.ExecutionStates flags = NativeMethods.ExecutionStates.Continuous | NativeMethods.ExecutionStates.SystemRequired |
                                                        NativeMethods.ExecutionStates.DisplayRequired;

            NativeMethods.ExecutionStates previousState = NativeMethods.SetThreadExecutionState(flags);
            ThrowWhenFailed(previousState);
        }
 /// <summary>
 /// Resets system idle timers and enables power management to function as configured in Windows.
 /// </summary>
 public static void Restore()
 {
     NativeMethods.ExecutionStates previousState = NativeMethods.SetThreadExecutionState(NativeMethods.ExecutionStates.Continuous);
     ThrowWhenFailed(previousState);
 }