Search code examples
windows-7taskschedulersleep

Close running app when my laptop goes to sleep in Windows 7 (close the lid or sleep button is pressed)


I have a program (program.exe) which I want to be closed when my laptop goes to sleep either if the sleep button was pressed, the lid was closed or some time has passed and entered automatically in sleep mode.

I suspect is something from the Task Scheduler but I don't know how to find event "When laptop goes to sleep" and then to close program.exe

I know that maybe I can do a Python script but I feel Task Scheduler it's the easiest way and maybe someone else, like me, needs to fix the same challenge.

Thanks!


Solution

  • After some digging, I found the solution I will post it here maybe it helps someone at some moment.

    1. Open Task Scheduler
    2. Create Task
    3. Give it a name (Close_program_on_sleep)
    4. Check Run whether user is logged on or not
    5. Select Configure for Windows 7
    6. Go on Triggers tab and then select New
    7. Select from Begin the task: On an event
    8. Select Log = System Source = Kernel-Power Event ID = 42. Click ok
    9. On Actions tab click new and then New> Start a program
    10. On the Program/Script add taskkill /f /im "your_program_to_be_closed.exe"
    11. Ok
    12. Enjoy :)!