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?


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.


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:


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.

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.