OnExit [, Label]
OnExit(Func [, AddRemove])  ; Requires [v1.1.20+]



如果省略, 则脚本会返回到正常的退出行为. 否则, 请指定 标签 的名称, 当脚本由于任意原因退出时将执行标签中的内容 (作为新 线程).


A function name or function object to call when the script is exiting. The function can optionally define parameters as shown below. If an OnExit function returns a non-zero integer, the script does not exit. Otherwise, the script exits after all registered functions are called.

ExitFunc(ExitReason, ExitCode)

One of the following values:
1 (the default): Call the function after any previously registered functions.
-1: Call the function before any previously registered functions.
0: Do not call the function.

If a label (subroutine) has been registered, it is always called first.


IMPORTANT: Since the specified subroutine is called instead of terminating the script, that subroutine must use the ExitApp command if termination is desired. [v1.1.20+]: New scripts should use a function instead of a subroutine -- this reduces the risk of accidentally creating a script which can't be exited, and ensures that the exit code passed to Exit or ExitApp is preserved.

[v1.1.20+]: Any number of OnExit functions can be registered. If a label (subroutine) is also registered, the functions are called after the subroutine calls ExitApp. An OnExit function usually should not call ExitApp; if it does, the script terminates immediately.

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

A script can detect and optionally abort a system shutdown or logoff via OnMessage(0x11, "WM_QUERYENDSESSION").

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, the OnExit subroutine 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. This prevents a flawed OnExit subroutine from making a script impossible to terminate.

If the OnExit subroutine was launched due to an Exit or ExitApp command that specified an exit code, in v1.1.19 and earlier that code is ignored and no longer available. In [v1.1.20+] the initial exit code is used unless overridden by calling ExitApp with a new exit code.

Whenever the OnExit subroutine is called by an exit attempt, it starts off fresh with the default values for settings such as SendMode. These defaults can be changed in the auto-execute section.

内置变量 A_ExitReason 为空,除非 OnExit 子程序正在运行或者在之前的退出中至少调用过一次。如果不是空的, 则它为下列单词的其中一个:

logoff 用户正在注销.
Shutdown 正在关闭或重启系统, 例如使用 Shutdown 命令.
Close 脚本接收到 WM_CLOSE 或 WM_QUIT 消息, 出现致命错误或者正被以其他方式关闭. 尽管这些情况都是很少见的, 然而 WM_CLOSE 可能是由脚本主窗口使用 WinClose 命令发出的. 要避免这种情况,请使用 Send, !{F4} 关闭主窗口。
错误 在没有热键且不是 持续运行的 脚本中发生了运行时错误. 运行时错误的一个例子是 Run/RunWait 命令无法启动指定的程序或打开指定的文档.
Menu 用户在主窗口的菜单或标准托盘菜单中选择了退出.
Exit 使用了 ExitExitApp 命令 (包括 自定义菜单项).
Reload 正通过 Reload 命令或菜单项重载脚本.
Single 由于 #SingleInstance 的结果, 脚本正被它自身的新实例代替.


OnMessage(), RegisterCallback(), OnClipboardChange, ExitApp, Shutdown, #Persistent, Threads, Gosub, Return, Menu


The following examples use #Persistent to prevent the script from exiting automatically. After running the script, right click the tray icon and click Exit to test the OnExit subroutine or function. Then click "Yes" to terminate the script or "No" to keep it running.

OnExit, ExitSub

if A_ExitReason not in Logoff,Shutdown  ; 在这行语句中, 注意不要在逗号周围含有空格.
    MsgBox, 4, , Are you sure you want to exit?
    IfMsgBox, No
ExitApp  ; A script with an OnExit subroutine will not terminate unless the subroutine uses ExitApp.

; Register a function to be called on exit:

; Register an object to be called on exit:
OnExit(ObjBindMethod(MyObject, "Exiting"))

ExitFunc(ExitReason, ExitCode)
    if ExitReason not in Logoff,Shutdown
        MsgBox, 4, , Are you sure you want to exit?
        IfMsgBox, 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.

class MyObject
        MsgBox, MyObject is cleaning up prior to exiting...