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

07
2013-11
  • Normadize

    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.

  • Answers
  • homay2

    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:


  • Related Question

    How can I prevent Windows from going to sleep during a scheduled task?
  • Kevin

    I have created scheduled tasks on both a Vista and Win7 machine which wake up the machine if it is currently in a sleep state. However, if the task takes longer than the power settings are configured on the machine, the machine will return to a sleep state. It looks like this utility might be a solution, but I was wondering if there were other options.

    Does anyone know of a powershell command or other utility that will prevent my machine from going into a sleep state while a task is running?


  • Related Answers
  • AnonJr

    There should be a checkbox in the options for scheduling a task. I don't have Win 7 in front of me at the moment so I can't say exactly where, just that I've used it before.

  • Smoove

    There is a setting in Scheduled Tasks that you can check or uncheck which will cause a task or script to exit if it runs longer than the time specified by the checked setting. For example, if you have a task which wakes up your computer at 4 AM to do windows updates, virus scans, etc, the task will not finish if it takes longer than the time set for it at Scheduled-Tasks->Task.Name->Properties->Settings. If you have this time set for a shorter duration than the task needs, it may appear that the task failed to finish because the computer went into standby while the task was still running, but the real reason is that a longer time has to be set. Or the box can be unchecked so there is no time constraint at all.

  • Raystafarian

    In task scheduler go to properties - conditions tab - select 'Wake the computer to run this task'. In W7 this also triggers the computer to not go to hibernate if the task is running

    edit- as commenters have pointed out, this bug may not be fixed. I haven't experienced it which is why I figured it was fixed, but there are reports that it wasn't fixed between vista and 7, which is what the OP indicates. Three is a thread over on Acronis with a script you can create and run with tasks that will stop the computer from hibernating. Additionally, there's a post on this website about inserting ping into your tasks to stop the computer from hibernating.

  • dmarietta

    The simplest way I have found to easily switch power modes to manage the machines ability to manage power is by defining Power Schemes. You just go into control panel and create any new power schemes or edit the existing existing predefined schemes. Setup a scheme for how you want the machine to behave normally where it does go to sleep when allowed. For example, on my Windows 7 machines I just edit the predefined "Balanced" power scheme for this. Then edit or create a scheme where the machine runs at full speed / power 100% of the time and is not allowed to sleep. Again, I just edit the existing "High Performance" power scheme and configure it that way. Then once these power schemes are defined you can easily script the mode the machine is running in using the POWERCFG.EXE command line utility.

    Pop open a command line window and run:

    powercfg -list
    

    ... when that runs, you should get a list of all of the machines defined power schemes along with the corresponding GUID value that you will need to note. Then to switch the machine from one power scheme to another, I have a simple batch file / script that I can run which passes the GUID for the scheme I want the machine to be running in. This way in my scheduled tasks and other tools, I can just call one of my scripts to keep the machine running without hibernating or at full speed. The batch file just uses powercfg.exe with the setactive switch to pick which power scheme I want. So this will look something like:

    powercfg -setactive 381b4222-f694-41f0-9685-ff5bb260df2e
    

    Just note that the last part is the GUID for your machine that you get by using the above list command.