OnExit

Causes the specified function to be called automatically when the script exits.

OnExit Function , AddRemove

Parameters

Function

Type: Function Object

The function object to call when the script is exiting. The function's parameters and return value are described below.

AddRemove

Type: Integer

If omitted, it defaults to 1 (call the function after any previously registered functions). Otherwise, specify one of the following numbers:

Function

The function should accept two parameters:

FunctionName(ExitReason, ExitCode)
ExitReason

Type: String

One of the following words:

Word Meaning
Logoff The user is logging off.
Shutdown The system is being shut down or restarted, such as by the Shutdown function.
Close

The script was sent a WM_CLOSE or WM_QUIT message, had a critical error, or is being closed in some other way. Although all of these are unusual, WM_CLOSE might be caused by WinClose having been used on the script's main window. To close (hide) the window without terminating the script, use WinHide.

If the script is exiting due to a critical error or its main window being destroyed, it will unconditionally terminate after the OnExit thread completes.

If the main window is being destroyed, it may still exist but cannot be displayed. This condition can be detected by monitoring the WM_DESTROY message with OnMessage.

Error A runtime error occurred in a script that is not persistent. An example of a runtime error is Run/RunWait being unable to launch the specified program or document.
Menu The user selected Exit from the main window's menu or from the standard tray menu.
Exit The Exit or ExitApp function was used (includes custom menu items).
Reload The script is being reloaded via the Reload function or menu item.
Single The script is being replaced by a new instance of itself as a result of #SingleInstance.
ExitCode

Type: Integer

An integer between -2147483648 and 2147483647 that is returned to its caller when the script exits. This code is accessible to any program that spawned the script, such as another script (via RunWait) or a batch (.bat) file. Zero is traditionally used to indicate success.

Return Value

The function can return a non-zero integer to prevent the script from exiting (with some rare exceptions) and subsequent functions from being called. Otherwise, the script exits after all registered functions are called.

Remarks

Any number of OnExit functions can be registered. An OnExit function usually should not call ExitApp; if it does, the script terminates immediately.

OnExit functions are called when the script exits by any means (except when it is killed by something like "End Task"). It is also called whenever #SingleInstance and Reload ask a previous instance to terminate.

A script can detect and optionally abort a system shutdown or logoff via OnMessage(0x0011, On_WM_QUERYENDSESSION) (see OnMessage example #2 for a working script).

The OnExit thread does not obey #MaxThreads (it will always launch when needed). In addition, while it is running, it cannot be interrupted by any thread, including hotkeys, custom menu items, and timed subroutines. However, it will be interrupted (and the script will terminate) if the user chooses Exit from the tray menu or main menu, or the script is asked to terminate as a result of Reload or #SingleInstance. Because of this, an OnExit function should be designed to finish quickly unless the user is aware of what it is doing.

If the OnExit thread encounters a failure condition such as a runtime error, the script will terminate.

If the OnExit thread was launched due to an Exit or ExitApp function that specified an exit code, that exit code is used unless an OnExit function returns true (preventing exit) or calls ExitApp.

Whenever an exit attempt is made, each OnExit function starts off fresh with the default values for settings such as SendMode. These defaults can be changed during script startup.

OnError, OnMessage, CallbackCreate, OnClipboardChange, ExitApp, Shutdown, Persistent, Threads, Return

Examples

Asks the user before exiting the script. To test this example, right-click the tray icon and click Exit.

Persistent  ; Prevent the script from exiting automatically.
OnExit ExitFunc

ExitFunc(ExitReason, ExitCode)
{
    if ExitReason != "Logoff" and ExitReason != "Shutdown"
    {
        Result := MsgBox("Are you sure you want to exit?",, 4)
        if Result = "No"
            return 1  ; OnExit functions must return non-zero to prevent exit.
    }
    ; Do not call ExitApp -- that would prevent other OnExit functions from being called.
}

Registers a method to be called on exit.

Persistent  ; Prevent the script from exiting automatically.
OnExit MyObject.Exiting

class MyObject
{
    static Exiting(*)
    {
        MsgBox "MyObject is cleaning up prior to exiting..."
        /*
        this.SayGoodbye()
        this.CloseNetworkConnections()
        */
    }
}