Jump to content

Proper way to close an application

Recommended Posts

I want to close all applications that were run from a USB HDD (portable apps) before dismounting the drive.

I use this code:

Func kill_process($process_name="")
; Kill the process if active, example: kill_process("notepad.exe")
    $PID = ProcessExists($process_name); Will return the PID or 0 if the process isn't found.
    If $PID Then 
    ; Waits until no instance of the process exists


This works, but I am not happy with it, because it is a brutal way of closing applications.

For example, some applications might ask me to confirm before closing, or perform various operations such as releasing the database. Just killing the process may result into some corrupt data.

What is the proper way to close an application in Autoit, based on its process name?

Note: I do not want to specify the name of the window, as it may change depending on the state of the program. Some applications may not even have any window open, just an icon in the system tray.

I prefer to use the process name (example: "notepad.exe"), but to do something less brutal than ProcessClose.

Link to comment
Share on other sites

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Thanks for this great link!

ProcessQuit() has roughly the same visible result as killing the process with ProcessClose(): applications close without running their closing tasks (such as displaying a confirmation pop-up or properly closing the database).

ProcessGetWindows() on the other hand is very useful. I was surprised to see that for each single process, there are many windows. Most of these windows are not even visible, especially when the application is in the system tray. For example, for the process "SCiTE.exe", 3 windows are reported

C:\myfile.txt - SciTE



In this case, closing the 1st window with WinClose() does the trick, but depending on the application, it is not always the 1st window in the list that must be closed in order to make a clean close of the application.

So I end up making a loop to close all windows.

- Is this the proper way to do it?

- Is there a way to identify the key window to be closed for each process?

Note: I tried WinGetState(), but I am a bit confused with the values returned by this function.

Here is my code:


    $WindowTitle = ProcessGetWindows($process_name)

    $array_dim = Ubound($WindowTitle)

    for $i = 0 to $array_dim-1
    ProcessWaitClose($process_name) ; Wait till the process disappears from the process list
Link to comment
Share on other sites

[edit] Worse: in some cases, closing the window with WinClose() is not enough to close the process. For example, the process "volumouse.exe" (download it at Nirsoft) reports 1 window only. WinClose () closes the window without problem, but "volumouse.exe" stays in the process list.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...