Sign in to follow this  
Followers 0
ppat

Proper way to close an application

4 posts in this topic

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 
        ProcessClose($PID)  
    ; Waits until no instance of the process exists
        ProcessWaitClose($process_name)
    EndIf

EndFunc

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.

Share this post


Link to post
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

Share this post


Link to post
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

DirectorExtension

SciTE_SingleThreadExtension_Dispatcher

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:

$process_name="SCiTE.exe"

    $WindowTitle = ProcessGetWindows($process_name)

    $array_dim = Ubound($WindowTitle)

    for $i = 0 to $array_dim-1
        WinClose($WindowTitle[$i][0])        
    Next
    
    ProcessWaitClose($process_name) ; Wait till the process disappears from the process list

Share this post


Link to post
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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0