Gyula
Gyula

Reputation: 73

Closing an application (fast)

I am creating a loop which needs to open and close fiels often. Furthermore this needs to be done quickly. I have used pywinauto to do this, yet it does not seem to work consistant enough.

import pywinauto
from time import sleep
import ctypes
import os

os.startfile(file_path)

# a bunch of keyboard events, which opens a seccond  window 

ctypes.windll.user32.keybd_event(0x12, 0, 0, 0) #Alt
ctypes.windll.user32.keybd_event(0x73, 0, 0, 0) #F4
ctypes.windll.user32.keybd_event(0x73, 0, 0x0002, 0) #F4
ctypes.windll.user32.keybd_event(0x12, 0, 0x0002, 0) #Alt
sleep(0.2)

Now my seccond window is closed, I still need to close the first window. I can not do this with keyboard strokes, since the window is not selected. currenly I use this to close the window:

app = pywinauto.application.Application(backend="uia") 
app.connect(path="myapplication.exe")
app.kill()

This method often works. However, when I loop the code a large number of times, finding and connecting the window can sometimes fail. I am looking for a reliable method to close the file, and I would also like it to be fast.

Upvotes: 1

Views: 5109

Answers (1)

Gyula
Gyula

Reputation: 73

This solution seems to suit me well.

os.system("TASKKILL /F /IM Myapplication.exe")

Upvotes: 2

Related Questions