In this short article I am going to show you how to achieve hibernation or standby of Windows programmatically.
The .NET 2.0 framework introduced the Application.SetSuspendState()
method, which allows you to either hibernate or else standby your pc.
This method accepts three parameters – the first parameter “state” is a PowerState
enumeration value of either Hibernate or Suspend.
The second parameter “force“, is a boolean
value, which when set to true forces the hibernation or standby immediately, and when set to false causes Windows to send a suspend request to every application. It is advisable to use false for this parameter because this allows any running applications to stop gracefully instead of being forced to stop immediately by Windows.
The third parameter “disableWakeEvent” is also a boolean
value. It specifies whether a wake event, such as remote wake-up call over the network, will allow the pc to wake up from standby or hibernation. The value true does not allow the pc to wake up, while false does allow a wake up.
This is the definition of the SetSuspendState()
method:
public static bool SetSuspendState( PowerState state, bool force, bool disableWakeEvent )
Below is an example showing how to hibernate Windows. The hibernation is not forced and the pc will recover on wake events:
Application.SetSuspendState(PowerState.Hibernate, false, false);
This next example shows how to put Windows into standby mode. Standby is forced and wake events are ignored:
Application.SetSuspendState(PowerState.Suspend, true, true);
Remember that when testing this code your pc will hibernate or go into standby mode, so make sure to back up your work before testing.
Although I’m sure you do backup your work regularly… right? 🙂
Dave
I need to put windows into sleep mode through a windows service.
I think this works only with windows application. Is there any way through which I can put it in sleep mode through windows service code?
Sorry for the above I meant standby mode