4

I have a Python script which performs a certain task and then goes to sleep for 4 hours. The task is to close a window which pops up every 4 hours.

while(True):
    if (is_window_open()):
        close_window()
        time.sleep(4*3600) #The timer is actually a little less than 4 hours
    time.sleep(1)

The script works perfectly when my PC running continuously, but it fails after Windows goes to sleep and wakes up. I realised that the sleep timer doesn't run when Windows is in sleep, and it continues the sleep timer from where it stopped when Windows went to sleep. The problem I'm facing is that when Windows wakes up from sleep, this window pops up, and I would like my script to close it automatically.

For example: Assume this window popped up at 2300 hrs and my script closed the window and went to sleep. Now at 2330 hrs, Windows went to sleep. Now the sleep timer stops at 30 mins. Assume at 0600 hrs, I wake up Windows from sleep. The window which is supposed to be closed appears but my script timer starts from 30 mins and has to complete another 3.5 hours before it can close this window.

One solution which popped up in my head is to check the actual sleep time and wake up time, and compare both those times to determine if 4 hours have elapsed or not. But this would require my script to know when Windows goes to sleep and when it wakes up, which I believe can be achieved using Windows Shutdown Events, although I couldn't figure out a way on how to use it. I saw this question, but couldn't figure out on how to use it in my scenario nor did it work when I ran it. I added a log when shutdown happened but that log never printed. In my scenario where Windows goes to sleep, I will need to listen to WM_POWERBROADCAST with PBT_APMSUSPEND but that also doesn't seem to be working. (I believe this could be because my script runs in the background)

Another feasible solution would be to make the sleep time to 1 second which would also solve the issue at hand, but I would like to know if there is an alternate solution where in I can keep the timer to 4 hours and still be able to make my script work.

Is there a solution for this wherein I can fix the idea where I can get the Windows sleep time and wake up time or maybe some other solution which I can use?

NOTE: I can't stop the window from popping up every 4 hours as I don't have the privileges to stop the Powershell script which makes the window pop up every 4 hours. Also, my Python script starts on it's own when I start my PC and the script contains 4 threads, which does minor tasks. The window closing script is one of these threads. My script runs in the background by using a Visual Basic script.

Set WshShell = CreateObject("WScript.Shell" ) 
WshShell.Run chr(34) & "D:\scripts\script.bat" & Chr(34), 0 
Set WshShell = Nothing 

script.bat just launches the python script.

EDIT: To check if the window is open and to close the window I use pygetwindow module.

mtm
  • 504
  • 1
  • 7
  • 25
  • you can use schedular instead of running an infinite loop. https://docs.python.org/3/library/sched.html – priyam bajpai Jul 26 '21 at 04:08
  • @priyambajpai But I want the loop to run infinitely. Assuming that I never shut down my PC (worst case, highly unlikely), I want to keep on checking every 4 hours. Even if I do it using scheduler, I would still have to run it every 4 hours. Can you elaborate on how to remove the while loop and still get the required effect using Event schedulers? – mtm Jul 26 '21 at 08:53

1 Answers1

0

Instead of letting the system handle the 4-hour sleep, you can take the matter into your own hand by sleeping for only 1 second at a time in a while loop until the current time reaches the scheduled end time:

end_time = time.time() + 4 * 3600
while time.time() < end_time:
    time.sleep(1)
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • 1
    Yes, I have mentioned a similar solution in my question. In my solution rather than sleeping for 4 hours, I would check if the window is open every one second, and that would solve the problem, even on sleep-wake up. But I would like to avoid the one second sleep and hence the question. – mtm Jul 26 '21 at 04:45