SoFunction
Updated on 2025-04-08

C# prevents shutdown of monitor and system standby when running programs

Recently I wrote a download program and found a problem: when I download it on the hang-up, the download task will be terminated due to the system hibernation.

My solution at first was to turn off hibernation, but later I found that this strategy was not very good: after the download was completed, if you continue to turn on the power, you waste power.

Therefore, the best way is to prevent the system from hibernating when downloading. That is, the download task will not be terminated due to sleep, and it will automatically switch to sleep state after downloading is completed, and there is no need to use the automatic shutdown function after downloading is completed.

After checking the relevant articles, you can use this SetThreadExecutionState API to prevent the system from hibernation. It is declared in C# as follows:

    [DllImport("")]
    static extern uint SetThreadExecutionState(ExecutionFlag flags);

    [Flags]
    enum ExecutionFlag : uint
    {
        System = 0x00000001,
        Display = 0x00000002,
        Continuous = 0x80000000,
    }

It only has a flags parameter, which has three options and can be used in combination. The main explanations are as follows:

  • When using only the Continuous parameter, it is the system sleep policy to restore.

  • When the Continuous parameter is not used, prevent the system from sleeping or the monitor from shutting down once.

  • When using Continuous parameters in combination, prevent the system from sleeping or the display from shutting down to thread termination

Therefore, there are three ways to use it, here I encapsulate it:

    class SystemSleepManagement
    {
        //Define API functions        [DllImport("")]
        static extern uint SetThreadExecutionState(ExecutionFlag flags);

        [Flags]
        enum ExecutionFlag : uint
        {
            System = 0x00000001,
            Display = 0x00000002,
            Continuous = 0x80000000,
        }

        /// <summary>
        ///Block the system to hibernate until the thread ends and restores the hibernation policy        /// </summary>
        /// <param name="includeDisplay">Doesn't stop the display from being turned off?</param>        public static void PreventSleep(bool includeDisplay = false)
        {
            if (includeDisplay)
                SetThreadExecutionState( |  | );
            else
                SetThreadExecutionState( | );
        }

        /// &lt;summary&gt;
        ///Restore system hibernation strategy        /// &lt;/summary&gt;
        public static void RestoreSleep()
        {
            SetThreadExecutionState();
        }

        /// &lt;summary&gt;
        ///Reset the system sleep timer        /// &lt;/summary&gt;
        /// <param name="includeDisplay">Doesn't stop the display from being turned off?</param>        public static void ResetSleepTimer(bool includeDisplay = false)
        {
            if (includeDisplay)
                SetThreadExecutionState( | );
            else
                SetThreadExecutionState();
        }
    }

Therefore, to prevent the program from sleeping during download, there are two ways to implement it:

  • The timer executes the ResetSleepTimer function regularly from the download period.

  • Execute the PreventSleep function at the beginning of the download, and execute the RestoreSleep function after the download is finished.

This is all about this article about preventing the display from turning off and system standby when running a program in C#. I hope it will be helpful to everyone's learning and I hope everyone will support me more.