Hibernate or Standby Windows Programmatically

Operating System

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? 🙂


DaveOnCSharp.com runs on the Thesis Theme

How smart is your Theme? How good is your support? Check out ThesisTheme for WordPress.

The Thesis Theme is one of the best Wordpress frameworks out there, especially if you understand software development, which since you're here you probably do. Thesis allows you to design post and page templates using a drag-and-drop system which gives you access to all the relevant page elements you would normally have to manually access through code. This is why understanding development techniques definitely helps, even though you don't need to write any code to get Thesis up and running.

So go check out the incredible features at DIYThemes and start using Thesis now!

2 comments… add one
  • Pramod

    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?

  • Pramod

    Sorry for the above I meant standby mode

Leave a Comment