1

I've trying to build a program which allows you to turn off or send a computer to sleep after a user defined set of time.

I had no issue with turning the computer off, but am having difficulty sending it to sleep (after 60 minutes for example)

I have the command to send the computer to sleep but I am having difficulty setting the time in which the program waits before it happens:

powercfg -h off & start /min "" C:\WINDOWS\System32\rundll32.exe PowrProf.dll,SetSuspendState 0,1,0 & ping -n 3 127.0.0.1 > nul & powercfg -h on & exit

Please point me in the right direction.

  • The make shift timer is set to 3 seconds as -n 3 will take 3 seconds to perform set it to 3600 and test – 50-3 Sep 16 '13 at 22:20
  • 1
    I would suggest changing the Vbs or powershell if you want easy to control delays – 50-3 Sep 16 '13 at 22:21
  • More of a programming question and better suited for SO than here . . . – ernie Sep 16 '13 at 22:47
1

Your question has little to do with python, as the command you've given in just command line on Windows, including the makeshift timer. Looking over the code you've shown, it's just a bunch of command line commands strung together with & . . .

powercfg -h off & start /min "" C:\WINDOWS\System32\rundll32.exe PowrProf.dll,SetSuspendState 0,1,0 & ping -n 3 127.0.0.1 > nul & powercfg -h on & exit

Breaking this down, the command does:

  1. powercfg -h off : turns off hibernation
  2. start /min "" C:\WINDOWS\System32\rundll32.exe PowrProf.dll,SetSuspendState 0,1,0 - I'm not 100% sure on this : puts the computer to sleep
  3. ping -n 3 127.0.0.1 > nul : this is a common timer hack for Windows, where it basically introduces a 3 second delay; I'm going to guess this is to handle some timing issues when a system is waking up from sleep, as this command won't run till you're re-waking
  4. powercfg -h on : re-enables hibernation
  5. exit : exits the batch file.

So, there's no delay here . . . to add a delay in python, you can use the time module's sleep function. Some example code would be something like:

import time
# Wait 10 seconds before running the next command
delay = 10
time.sleep(delay)
print "hi . . . it's been {0} seconds!".format(delay)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.