Scheduling a task before sleep in Windows 7 and hold off the sleep event until task runs

Note that this is different than the other questions I found (I did search), like: Is there any way to execute something when closing the laptop's lid?

I used the same trick of scheduling a task on event #42 (System, Kernel-Power, Event 42), but the problem is that the task - at least in my case - is not actually run before going to sleep. it seems the schedule executes it and goes to sleep at the same time. if the task takes longer to execute, then tough luck.

I have a batch file which does:

@echo off
cd /d %~dp0
taskkill /t /im skype.exe
sleep 2
taskkill /t /f /im skype.exe
sleep 1

My task is not executed before going to sleep. The machine goes to sleep immediately as I can see the LEDs going off and hear the HDD click in less than 1 second, whereas I wanted it to hold of for 3 seconds before going to sleep.

It seems that tasks started by the scheduler are not holding off the sleep event -- and there are valid reasons for that (what if your task hangs?) -- but in my case, this is bummer!

Anyone knows of a way to make it work as intended above but being triggered by event 42?

Cheers

NOTE: I know I can create a batch/program/task which first kills skype, waits, and then sends a Sleep event but I would like to be able to trigger it using Event 42. Other users sometime log onto this machine while Skype is running in a different account and they send it to sleep using the normal Sleep entry in the start menu, not a custom batch/program.

NOTE2 also tried with full paths for taskkill and sleep which are in Windows\System32. This kills Skype.exe then waits for 2 seconds then forcefully kills Skype.exe and then waits for another 1 second. This is due to a bug in Skype for Win 7 x64 which hangs Skype after waking up from Sleep. rather than waiting for Microsoft to fix this very old and frustrating and oh so overly-reported bug, I wanted to implement my own fix.


Solution 1:

Event 42 occurs too late (at the end of the process, no time to do anything useful) because it is intended as a "documentation" for subsequent diagnostics, not a notification before things happen. (I don't understand how could a MSFT moderator advice it?)

Unfortunately I haven't seen a working solution to your problem, but I'm sure that Windows does provide a before-event notification, although not in the Event log. This means that you have to create/use an always-running background application (~ a "daemon") that catches this pre-standby notification and then does whatever you like.
I'm not aware of an existing application that can do this, neither I know the relevant Windows APIs; but until a better answer is provided, here are some starting points that may help you in working out a solution:

  • The following MSDN pages seem to lead to information about the notification itself: http://msdn.microsoft.com/en-us/library/windows/desktop/aa373233
    http://msdn.microsoft.com/en-us/library/ms703398.aspx#handling_sleep_transitions
    http://msdn.microsoft.com/en-us/library/ms701723.aspx
  • If you don't want to dig into C++, then the start_power_monitor command of the TWAPI extension of Tcl/Tk looks promising (see the apmsuspend event).
    Tcl/Tk is not a complex product requiring installation and configuration, but rather a plain, very powerful executable that, when coupled with the single TWAPI .dll, competes with PowerShell (with much lighter footprint) (Although surely PowerShell could cope with the problem, too, just I don't know the relevant commands/packages.)

UPDATE:

There's no way to “hold off the sleep event” because Microsoft has discontinued PBT_APMQUERYSUSPEND that allowed it in XP and Vista. Instead, now says that you cannot prevent the user from putting the computer to sleep:

Applications should respect that the user expects a certain behavior when they close the lid on their laptop or press the power button.

Only the PBT_APMSUSPEND event remained, after which

The system allows approximately two seconds for an application to handle this notification.