Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make my service keep Windows from going to sleep?

I have a Windows service that needs to run on a PC that is left on 24 hours.

I can't rely on the PC having sleep/shut down disabled because it is something being installed on around 3500 sites and X, Y or Z might mean that sleep/shut down is not disabled.

Is there some neat .NET way I can keep Windows from snoozing?

Or would periodically writing to a file (say writing the date once a minute) be suffice?

Please no lateral/bad practice warning "you shouldn't be forcing that sort of thing, leave it up to the computer" answers. It's my job to make sure this program achieves this on the customer's computers!

Cheers!

Edit:

[Big sigh]

As usual the laterals can't help but comment. I did try to dissuade to avoid having to spell out-justify myself, but hey. It didn't work. People assumed I'm trying to take over "THEIR" computers.

It is for a corporate customer and it monitors actions on security hardware and logs it. Like someone opening a door with an electronic key.

As the doors need to be functioning 24 hours a day, they obviously want logging 24 hours a day.

It's not a crime. They have a lot of sites where they want this to happen. They can't rely on staff there turning the hibernate/sleep features off. So they asked me to make sure it can stay alive.

Again, it really isn't a crime. Sometimes these things just have to happen.

I hate having to justify every question I ask on here. The real world just isn't as neat and fluffy as we'd all like it to be.

On a more positive note, big thanks to those who helped!!

like image 906
joshcomley Avatar asked Oct 27 '09 13:10

joshcomley


People also ask

How do I force Windows to stay awake?

Go to Control Panel > Personalization > Change Screensaver. Next to On Resume, Display Logon Screen, uncheck the box. This prevents your system from sleeping.


2 Answers

Have a look at the SetThreadExecutionState API. It enables you to notify the system that your application is active so that the computer doesn't go to sleep

like image 112
Thomas Levesque Avatar answered Nov 15 '22 05:11

Thomas Levesque


Yes, there is! Other applications are doing this (see Cyberlink's Power DVD for instance) so the API is there.

I suggest you start reading about Windows Power Management API's. However, you may have to resort to P\Invoke for this one as I don't think .NET provides managed implementations for those APIs.

In particular it seems there is a function to register your service for notifications of Power State changes: RegisterPowerSettingNotification. I'm assuming there may be a way for your service to request that certain power state changes be canceled (such as enter sleep state).

Then you should also look at these Power Management Functions.

And, as a last resort - and a hacky one nonetheless - you could make your service generate and send key presses to the system at a regular interval to simulate user activity.

** NOTE **

After giving this some more thought, I'd like to point out that the approach I would take is to create a Power Management Profile in Windows that configures the computer to never go to sleep or hibernate. And then, from your application/service, monitor the active power state profile (using the APIs above) and if it ever changes, programmatically change it back. This should be a pretty clean way of enforcing a policy. See PowerSetActiveScheme.

like image 24
Mike Dinescu Avatar answered Nov 15 '22 06:11

Mike Dinescu