5

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?

Thanks for any pointers.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Renaud Bompuis
  • 16,596
  • 4
  • 56
  • 86

2 Answers2

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);
Leon Tayson
  • 4,741
  • 7
  • 37
  • 36
  • 1
    See http://msdn.microsoft.com/en-us/library/system.windows.forms.application.setsuspendstate.aspx for the full details – John Dec 29 '08 at 16:39
  • Full method signature: Application.SetSuspendState([PowerState.Hibernate | PowerState.Suspend], bool force, bool disableWakeEvent) – Simon_Weaver Sep 15 '10 at 00:34
  • 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, false – Simon_Weaver Sep 15 '10 at 00:37
  • 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) – Simon_Weaver Sep 15 '10 at 00:47
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.

dsrdakota
  • 2,415
  • 1
  • 15
  • 10