views:

312

answers:

3

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!!

+2  A: 

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.

Miky Dinescu
While this answer is likely what joshcomley needs, I really hope that it's not possible with that API to block sleep/hibernation when the user forces it. Closing a laptop, putting it into the bag and then hours later discovering that the battery died with data loss is a thing from Windows XP days I hope I'll never see again (so far Vista and Seven did not do something like that).
OregonGhost
Well, I certainly understand your point of view OregonGhost but in a controlled environment I see nothing wrong with this. Of course only an idiot would try to prevent a laptop from hibernatig/sleeping but it doesn't seem like that's what the OP wanted.
Miky Dinescu
@Miky D - too bloody right. Spot on! Nice example of the DVD player - I get annoyed when I'm watching long online movies and the screen turns off after 20 mins. Sometimes such features are required.
joshcomley
@OregonGhost - This is quite possible from what I've seen. For example, when you are running a PowerPoint presentation, a laptop or PC will not go to sleep. (There are definite times this is needed.)
JasCav
+3  A: 

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

Thomas Levesque
no, it says "Minimum supported client : Windows 2000 Professional". But the ES_AWAYMODE_REQUIRED flag is indeed supported only in Vista (and later)
Thomas Levesque
Unless his client is one of those who refused to allow Vista on their machines (like where I work), I still think this is a good solution. If it doesn't work fully on some older machines, that's easy enough to explain. If you want to keep obsolete systems around, you should expect a bit more management work. Any new ones it should work on.
T.E.D.
This sounds promising. They use XP boxes - which is after Windows 2000, if I remember correctly!
joshcomley
+1  A: 

We faced a similar (but inverted) problem many moons ago with our emergency call handling systems: waking up a monitor that had gone to sleep in order that its associated integral speakers would play a ring tone. I found the best way was to use SendInput to programmatically jiggle the mouse one pixel and then back again. The code used the MOUSEEVENTF_MOVE with SendInput. This is native code rather than .Net, but it should give you somewhere to start looking.

Bob Moore