OnExit() / OnExit

Specifies a callback function or subroutine to run automatically when the script exits.

OnExit() [v1.1.20+]

OnExit(Func , AddRemove)

매개변수

Func

스크립트가 종료할 때 호출할 함수 이름 또는 함수 객체. 이 함수는 선택적으로 아래와 같이 매개변수를 정의할 수 있습니다. If an OnExit function returns a non-zero integer, the script does not exit (with some rare exceptions) and no more functions are called. 그렇지 않으면, 스크립트는 모든 등록된 함수를 호출한 후에 종료합니다.

ExitFunc(ExitReason, ExitCode)
AddRemove

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

라벨(서브루틴)이 등록되어 있으면, 그것이 언제나 먼저 호출됩니다.

논평

새 스크립트는 서브루틴 대신에 함수를 사용해야 합니다 -- 이렇게 하면 우발적으로 종료가 불가능한 스크립트를 만들어 낼 위험을 줄여 줍니다. 그리고 Exit 또는 ExitApp에 건넨 종료 코드가 보존된다는 확신을 할 수 있습니다.

얼마든지 OnExit 함수를 등록할 수 있습니다. 라벨 (서브루틴)도 등록되어 있다면, 함수는 서브루틴이 ExitApp를 호출한 뒤에 호출됩니다. OnExit 함수는 보통 ExitApp를 호출하면 안됩니다; 그럴 경우, 스크립트는 즉시 종료합니다.

OnExit

비추천: 이 명령어는 새 스크립트에 사용을 추천하지 않습니다. 대신 OnExit 기능을 사용하십시오.

OnExit , Label

매개변수

Label

생략하면, 이전에 등록된 라벨이 등록 해제됩니다. 그렇지 않으면 스크립트가 어떤 방식으로든 종료할 때 그의 내용이 (새 쓰레드로) 실행될 라벨의 이름을 지정하십시오.

논평

중요: 스크립트를 종료하는 대신에 지정된 서브루틴이 호출되기 때문에, 그 서브루틴은 종료가 필요하면 반드시 ExitApp 명령어를 사용해야 합니다.

내장 함수 A_ExitReason는 비어 있습니다. 단, OnExit 서브루틴이 현재 실행 중이거나 이전이 종료 시도로 인해 적어도 한 번 호출된 경우는 예외입니다. If not blank, it is one of the words from the table below.

논평

The OnExit callback function or subroutine is called when the script exits by any means (except when it is killed by something like "End Task"). 또한 #SingleInstance 그리고 Reload 명령어가 이전이 실체를 종료하도록 요구할 때마다 호출됩니다.

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

OnExit 쓰레드#MaxThreads를 준수하지 않습니다. (필요하면 언제든지 기동합니다). 게다가, 실행 중인 동안, 다른 쓰레드로 인터럽트 할 수 없습니다. 여기에는 핫키, 맞춤 메뉴 항목, 그리고 타이머 서브루틴이 포함됩니다. 그렇지만, 사용자가 메인 메뉴나 트레이 메뉴로부터 Exit을 선택했다면 인터럽트 됩니다 (그리고 스크립트는 종료합니다). 그렇지 않으면 스크립트에 Reload 또는 #SingleInstance 결과로 종료하도록 요구합니다. Because of this, the OnExit callback function or subroutine should be designed to finish quickly unless the user is aware of what it is doing.

OnExit 쓰레드가 실행시간 에러 같은 실패 조건을 만나면, 스크립트는 종료합니다. This prevents a flawed OnExit callback function or subroutine from making a script impossible to terminate.

If the OnExit thread 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. [v1.1.20+]에서 최초의 종료 코드가 사용됩니다. 단, ExitApp 호출하여 새 종료 코드로 오버라이드한 경우는 예외입니다.

Whenever an exit attempt is made, each OnExit callback function or subroutine starts off fresh with the default values for settings such as SendMode. 이런 기본 값은 자동-실행 섹션에서 바꿀 수 있습니다.

Exit Reasons

Reason 설명
Logoff 사용자가 로그오프 중입니다.
Shutdown 예를 들어 Shutdown 명령어로 시스템이 닫히고 있는 중이거나 재시작 중입니다.
Close

스크립트에 WM_CLOSE 또는 WM_QUIT 메시지가 전송되었습니다. 스크립트에 심각한 에러가 있습니다. 또는 다른 어떤 방법으로 닫혔습니다. 이 모든 것이 비정상적이지만, WM_CLOSE는 스크립트의 메인 창에 사용된 WinClose에 의해 야기될 수도 있습니다. 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 실행시간 에러가 핫키가 없고 영속적이 아닌 스크립트에 일어났습니다. 실행시간 에러의 예는 Run/RunWait가 지정된 프로그램이나 문서를 기동시키지 못하는 경우입니다.
Menu 사용자가 메인 창의 메뉴 또는 표준 트레이 메뉴로부터 로부터 Exit를 선택했습니다.
Exit Exit 또는 ExitApp 명령어가 사용되었습니다 (맞춤 메뉴 항목 포함).
Reload 스크립트가Reload 명령어나 메뉴 항목을 통하여 재적재 중입니다.
Single #SingleInstance의 결과로 스크립트가 자신의 새 실체로 교체 중입니다.

OnError(), OnMessage(), RegisterCallback(), OnClipboardChange(), OnClipboardChange 라벨, ExitApp, Shutdown, #Persistent, 쓰레드, Gosub, Return, Menu

예제

Function vs. command.

Despite the different approach, both examples have the same effect; that is, they ask the user before exiting the script. To test them, right-click the tray icon and click Exit.

#Persistent  ; Prevent the script from exiting automatically.
OnExit("ExitFunc")

ExitFunc(ExitReason, ExitCode)
{
    if ExitReason not in Logoff,Shutdown
    {
        MsgBox, 4, , 종료하고 싶습니까?
        IfMsgBox, No
            return 1  ; OnExit 함수는 종료를 막기 위해 반드시 0 아닌 값을 돌려주어야 합니다.
    }
    ; ExitApp를 호출하지 마십시오 -- 그러면 다른 OnExit 함수가 호출되지 못합니다.
}
#Persistent  ; Prevent the script from exiting automatically.
OnExit, ExitSub  
return

ExitSub:
if A_ExitReason not in Logoff,Shutdown  ; 이 줄에서 쉼표 둘레에 스페이스를 피하십시오.
{
    MsgBox, 4, , 종료하고 싶습니까?
    IfMsgBox, No
        return
}
ExitApp  ; OnExit 서브루틴이 있는 스크립트는 종료하지 않습니다. 단, 서브루틴이 ExitApp를 사용하는 경우는 예외입니다.

종료시에 호출돌 객체를 등록합니다.

#Persistent  ; Prevent the script from exiting automatically.
OnExit(ObjBindMethod(MyObject, "Exiting"))

class MyObject
{
    Exiting()
    {
        MsgBox, 종료하기 전에 MyObject가 청소 중입니다...
        /*
        this.SayGoodbye()
        this.CloseNetworkConnections()
        */
    }
}