Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2007-06-12
5
Medium Priority
?
6,312 Views
Last Modified: 2012-05-05
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.
0
Comment
  • 2
  • 2
5 Comments
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 19272441
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.
0
 
LVL 9

Accepted Solution

by:
cheddar73 earned 2000 total points
ID: 19272549
I have to disagree.  You can absolutely do this.  The Win32 API is called SetThreadExecutionState.

http://msdn2.microsoft.com/en-us/library/aa373208.aspx

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_SYSTEM_REQUIRED = &H1
        ES_DISPLAY_REQUIRED = &H2
        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
        Dim targetState As EXECUTION_STATE = EXECUTION_STATE.ES_CONTINUOUS Or EXECUTION_STATE.ES_SYSTEM_REQUIRED

        ' 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.

scrathcyboy,

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."
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 19272582
"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 !!!
0
 
LVL 9

Expert Comment

by:cheddar73
ID: 19272644
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
    SetThreadExecutionState(ES_SYSTEM_REQUIRED)
Loop

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.
0
 

Author Comment

by:RobertFromSecretWeapons
ID: 19280639
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.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Loops Section Overview

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question