First im Sorry for my Bad English.
I try to pause and resume a .exe File with Python. I Already tryed psutil.Process(...).suspend()/.resume() but this take more then 8 Seconds. Normally thats okay, but i freeze a WallpaperEngine and it looks very shit if your Background is Freezed. So i need a alternative for p.suspend() and p.resume().
Important Informations: -> OS: Windows 10 -> Python: 3.8
My Code:
from win32gui import GetWindowText, GetForegroundWindow
import win32gui
import time
import pygetwindow as gw
import psutil
process_name = "WallEngine"
pid = None
detectedPIDs = 0
for proc in psutil.process_iter():
if process_name in proc.name():
pid = proc.pid
detectedPIDs+=1
print(pid)
if detectedPIDs == 1:
pass
else:
print("Didnt Found the WallEngine Process. Enter Searching Mode ...")
while detectedPIDs != 1:
for proc in psutil.process_iter():
if process_name in proc.name():
print("Searching Mode Finished. We Found!")
pid = proc.pid
detectedPIDs+=1
print(pid)
print("[INFO] WallpaperEnginePerfomancer Booted.")
allWindows = []
def winEnumHandler(hwnd, ctx, currentlyPaused=None):
if not win32gui.IsWindowVisible(hwnd):
# print("Window isnt Visble yet! (" + str(win32gui.GetWindowText(hwnd)) + ")")
pass
else:
rect = win32gui.GetWindowRect(hwnd)
windowName: str = win32gui.GetWindowText(hwnd)
x = rect[0]
y = rect[1]
w = rect[2] - x
h = rect[3] - y
currentScannedWindow = gw.getWindowsWithTitle(windowName)[0]
if currentScannedWindow.isMaximized:
if windowName == "" or windowName == None or "Paint 3D" in windowName:
pass
else:
allWindows.append(windowName)
def window_handler():
p = psutil.Process(pid)
if len(allWindows) == 0:
p.resume()
else:
p.suspend()
allWindows.clear()
if __name__ == '__main__':
while True:
win32gui.EnumWindows(winEnumHandler, None )
window_handler()
time.sleep(0.09)