Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following code will not disable the power button, but if the device is turned off, it will turn the device back on again within 10 seconds. It will also disable any power saving functionality.</p> <pre><code>using System; using System.Runtime.InteropServices; using System.Collections.Generic; using System.Text; namespace Core.Mobile { /// &lt;summary&gt; /// Allows manipulation the power management i.e. system standby /// &lt;/summary&gt; public static class PowerManipulation { #region Private variables private static System.Threading.Timer _timer = null; private const int INTERVAL = 10000; //10 seconds #endregion #region Public methods /// &lt;summary&gt; /// Prevents the application from suspending/sleeping /// &lt;/summary&gt; public static void DisableSleep() { if (_timer == null) { _timer = new System.Threading.Timer(new System.Threading.TimerCallback(Timer_Tick), null, 0, INTERVAL); } try { PowerPolicyNotify(PPN_UNATTENDEDMODE, 1); //Ensure the application still runs in suspend mode } catch { } } /// &lt;summary&gt; /// Allows suspend/sleep operations /// &lt;/summary&gt; public static void EnableSleep() { if (_timer != null) { _timer.Dispose(); _timer = null; } try { PowerPolicyNotify(PPN_UNATTENDEDMODE, 0); } catch { } } #endregion #region Private methods /// &lt;summary&gt; /// Internal timer for preventing standby /// &lt;/summary&gt; private static void Timer_Tick(object state) { try { SystemIdleTimerReset(); SetSystemPowerState(null, POWER_STATE_ON, POWER_FORCE); } catch { } } #endregion #region PInvoke private const int PPN_UNATTENDEDMODE = 0x00000003; private const int POWER_STATE_ON = 0x00010000; private const int POWER_STATE_OFF = 0x00020000; private const int POWER_STATE_SUSPEND = 0x00200000; private const int POWER_FORCE = 4096; private const int POWER_STATE_RESET = 0x00800000; /// &lt;summary&gt; /// This function resets a system timer that controls whether or not the /// device will automatically go into a suspended state. /// &lt;/summary&gt; [DllImport("CoreDll.dll")] private static extern void SystemIdleTimerReset(); /// &lt;summary&gt; /// This function resets a system timer that controls whether or not the /// device will automatically go into a suspended state. /// &lt;/summary&gt; [DllImport("CoreDll.dll")] private static extern void SHIdleTimerReset(); /// &lt;summary&gt; /// This function allows the current power state to be manipulated, i.e. turn the device on /// &lt;/summary&gt; [DllImport("coredll.dll", SetLastError = true)] static extern int SetSystemPowerState(string psState, int StateFlags, int Options); /// &lt;summary&gt; /// This function sets any power notification options /// &lt;/summary&gt; [DllImport("CoreDll.dll")] static extern bool PowerPolicyNotify(int dwMessage, int onOrOff); #endregion } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload