Run program before hibernation/sleep


Recommended Posts

When i press the sleep-button on my computer i want to run a program before windows goes to sleep.

How to do?

I have tried hibernate trigger (http://www.desimonesystems.com/suspendtrigger/index.php) but it only starts the program before the hibernation starts, it doesn't wait for the program to finish (they say it should but maybe it doesn't work fully in vista).

My program has to finish (takes about 5-10sec) before windows go into sleep.

I don't care how advanced the solution is, i really really need this.

Link to comment
https://www.neowin.net/forum/topic/746664-run-program-before-hibernationsleep/
Share on other sites

Would executing a batch file work? Try experimenting with the start command, specifically "start /w". The /w flag tells it to wait for the program to finish executing.

For example, this batch file (save as "dir-hibernate.bat") would display a directory & then hibernate.

@echo off

start /w cmd /c "dir /a c:\windows"

shutdown /h /f

exit

Keep in mind, if the program you're executing before hibernate hangs up or waits for you to click/enter something, it's never going to hibernate since it'll be stuck waiting..I'm guessing you've already considered that :)

  lars77 said:
Would executing a batch file work? Try experimenting with the start command, specifically "start /w". The /w flag tells it to wait for the program to finish executing.

For example, this batch file (save as "dir-hibernate.bat") would display a directory & then hibernate.

@echo off

start /w cmd /c "dir /a c:\windows"

shutdown /h /f

exit

Keep in mind, if the program you're executing before hibernate hangs up or waits for you to click/enter something, it's never going to hibernate since it'll be stuck waiting..I'm guessing you've already considered that :)

That's actually almost what i'm doing now.

In my program that requires the action before sleep i've added a hibernate button so as long as i hibernate through the program everything is fine.

But i'm looking for a solution that works no matter how you put the computer to sleep. It's pretty stupid to have a computer that can't go to sleep after being idle X minutes or by using the normal sleep-buttons :(

You could maybe try monitoring Windows' system events, pretty much all power events will get logged in the event log. In this way you can catch pretty much all types of sleep/poweroff events. But I'm not sure if you can make make standby/hibernate "wait" until your program is done doing what it's doing (maybe if you somehow cancel it & then re-submit the standby/hibernate request afterwards?). These might be helpful:

http://nico.berlee.nl/#/on-standby-close-mce

http://msdn.microsoft.com/en-us/library/aa394362.aspx

Just be careful about any solutions that force the user to wait for their machine to sleep, or prevent it from sleeping altogether. eg: Users hate it when their laptop doesn't go to sleep when it was supposed to :p

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.