Can I do my maintenance so that Windows does not fall asleep?

I have a Windows service that should run on a PC that is left for 24 hours.

I cannot rely on a PC that has the shutdown / shutdown function disabled because it is something installed on 3,500 sites, and X, Y or Z may mean that sleep / shutdown mode is not turned off.

Is there some neat .NET way I can get Windows to delay?

Or will it be periodically written to a file (for example, write the date once a minute)?

Please do not report lateral / incorrect practices "you should not force such things, leave them on the computer." It is my job to make sure that this program achieves this on client computers!

Hooray!

Edit:

[Big sigh]

As usual, the sides cannot help but comment. I tried to dissuade, to avoid the need to explain, to justify myself, but hey. This did not work. People assumed that I was trying to get hold of "THEM" computers.

It is intended for the corporate client and monitors actions on security hardware and registers it. As someone opens the door with an electronic key.

Since the doors must function 24 hours a day, they obviously want to keep a journal 24 hours a day.

This is not a crime. They have many sites where they want this to happen. They cannot rely on personnel who disable hibernation / sleep functions. So they asked me to make sure that he could stay alive.

Again, this is really not a crime. Sometimes these things just have to happen.

I do not like to justify every question I ask here. The real world is just not as neat and fluffy as we all would like.

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

+4
source share
3 answers

Yes there is! Other applications do this (e.g. Cyberlink Power DVD), so an API exists.

I suggest you start reading the Windows Power Management API . However, you may have to resort to P \ Invoke for this, since I don't think .NET provides managed implementations for these APIs.

In particular, it seems that there is a function to register your service for notifications of changes in Power status: RegisterPowerSettingNotification . I assume that you might have a way to request the cancellation of certain changes in the power state (for example, enter a wait state).

Then you should also look at the Power Management Features .

And, as a last resort - and, nevertheless, hacked - you can force your service to generate and send keystrokes to the system at regular intervals to simulate user activity.

** NOTE **

Having thought about this, I would like to note that the approach I would like to take is to create a power management profile in Windows that configures the computer to never sleep or hibernate. And then, from your application / service, monitor the active power status profile (using the APIs above), and if it ever changes, programmatically change it. This should be a pretty clean way to enforce policy. See PowerSetActiveScheme .

+3
source

Take a look at the SetThreadExecutionState API. It allows you to notify the system that your application is active so that the computer does not fall asleep

+3
source

We faced a similar (but inverted) problem many years ago with our emergency call processing systems: waking up a monitor that fell asleep so that the integrated speakers connected with it played a ringtone. I found that the best way is to use SendInput to programmatically shift the mouse one pixel and then back. The code used MOUSEEVENTF_MOVE with SendInput. This is proprietary code, not .Net, but it should give you the opportunity to start your search.

+1
source

All Articles