Preventing Standby, Sleep modes from happening while an application runs.

When my application runs, I have a requirement to prevent a system from entering Standby, etc.  The reason for this is that it loses connection to the remote host.

So, while my application runs, I need to prevent any kind of standby event from occuring.  When my application ends, I need to resume whatever the state was of the power management system.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YOU CANNOT DO THIS !! -- power management is based on user settings -- NOT on applications running.

To disable power management, right click desktop, properties, screen saver, turn off all screen savers, and then click the power button, set everything in that screen to "always on", monitor, hard disks, and disable system standby.

Now just LEAVE IT THAT WAY -- this idea of windows power management is basically BS for a desktop -- you simply DO NOT need it -- the only time you need it is on a laptop on batteries.

If this is what your users have, you might as well forget it, there is no way to do this based on the running of an application, and reset it afterwards.  This is a setting RESERVED FOR THE USER to set for himself, based on how he wnats to use the computer, the apps cannot control this, the user does.
I have to disagree.  You can absolutely do this.  The Win32 API is called SetThreadExecutionState.

However, scrathcyboy has a point.  This API should be used responsibly, or the result could be dead batteries and angry users.

Here it is (didn't know if you wanted this in C# or VB):

    Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esflags As Integer) As EXECUTION_STATE

    Private Enum EXECUTION_STATE
        ES_CONTINUOUS = &H80000000
    End Enum

Then to call it:
        ' ES_CONTINUOUS means keep the execution state setting until the next time the function is called
        ' ES_SYSTEM_REQUIRED means reset the system idle timer so standby mode won't be triggered

        ' Function returns the previous state.  Save this value so you can set it back later.
        Dim previousState As EXECUTION_STATE = SetThreadExecutionState(targetState)

        ' Do whatever you need to do here.

        ' Put the execution state back the way you found it.
        Dim result As EXECUTION_STATE = SetThreadExecutionState(previousState)

Alternatively, you can omit ES_CONTINUOUS and just call it with ES_SYSTEM_REQUIRED every so often to reset the system idle timer.  Then you wouldn't have to worry about putting it back how you found it when you're finished.


I am sympathetic to what you are saying.  As I said above, this API needs to be used responsibly.  However, you might be interested in this snippet from the MSDN link above:

"The SetThreadExecutionState function cannot be used to prevent the user from putting the computer in standby mode. Applications should respect that the user expects a certain behavior when they close the lid on their laptop or press the power button."

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
"YOU CANNOT DO THIS !! -- power management is based on user settings -- NOT on applications running."

This is M$'s model for XP -- it is an inviobale rule they have set down, it is totally USER CONTROLLED !!!
But I just did it.  I set my power options to hibernate after 2 minutes.  Then I left the machine alone to make sure it would.  It did.

Then I ran this code:

Do While True

I came back after 10 minutes and the system was not hibernating.

I have read on some blog somewhere (which I can't find now) that it doesn't work on Vista, but it certainly does on XP.
RobertFromSecretWeaponsAuthor Commented:
This souldn't cause such a commotion.  There are times when it's important NOT to have the PC go into Standy.  It most surely does while my host connected application is running.  It's a real time data feed.  Once PC goes into Standy, host connection gets killed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.