We use taskkill.exe
in some of our automation test teardowns, some of which involve having to close a WPF application.
I've discovered that if more than one window is open, taskkill will kill the active window but not the entire application. We would prefer not to have to use the /F flag because that does not allow the application to close cleanly and leaves our database "dirty".
Has anybody run into this problem and found a solution, or have any ideas before we take a radically different approach? The closest I could find is setting Application.Current.ShutdownMode
to OnMainWindowClose
, but of course that doesn't help if the main window is not the front window when taskkill
is executed.
Maybe the Process class could help you.
Process[] p = Process.GetProcessesByName("appname");
p[0].CloseMainWindow();