How to programmatically bring a laptop into Sleep mode
Asked Answered
D

2

6

What API or tools can I use to query the capabilities of the system and choose the most appropriate on for putting the PC to Sleep, Hibernate or shutdown mode?

Dunham answered 29/12, 2008 at 16:20 Comment(0)
L
10

Look at SystemInformation.PowerStatus, then you can call Application.SetSuspendState to put the PC to Sleep or Hibernate like:

Application.SetSuspendState(PowerState.Hibernate, true, true);
Logarithmic answered 29/12, 2008 at 16:35 Comment(5)
See msdn.microsoft.com/en-us/library/… for the full detailsScriptural
Full method signature: Application.SetSuspendState([PowerState.Hibernate | PowerState.Suspend], bool force, bool disableWakeEvent)Jowers
depending upon the application needs i would recommend carefully considering the true, true parameters. sending true for the 'force' parameter may cause some applications or driver problems resuming because they won't be sent the suspend request. if you experience problems waking up try switching true, true to false, true or false, falseJowers
Note: this is in the System.Windows.Forms namespace, so if you're using a WPF Application you'll need to add a reference to System.Windows.Forms DLL and call it using System.Windows.Forms.Application.SetSuspendState(PowerState.Suspend, true, true)Jowers
Language is C# ?Antarctica
C
2

You could use the API:

Declare Function SetSuspendState Lib "PowrProf" (ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer

SetSuspendState(0, 0, 0) 'Sleep without forcing and ?no? wake events
SetSuspendState(1, 0, 0) 'Hibernate without forcing and ?no? wake events

Set Hibernate to 1(True) to Hibernate or 0(False) to Sleep.

See the API here.

Carloscarlota answered 11/11, 2012 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.