OnExit

Registers a function to be called automatically whenever the script exits.

OnExit Callback , AddRemove

パラメータ

Callback

型:機能オブジェクト

The function to call.

The callback accepts two parameters and can be defined as follows:

MyCallback(ExitReason, ExitCode) { ...

パラメータに与える名前は重要ではないが、以下の値が順次割り当てられる:

  1. The exit reason (one of the words from the table below).
  2. The exit code passed to Exit or ExitApp.

対応する情報が不要な場合、コールバックのパラメータリストの最後から1つ以上のパラメータを省略することができますが、この場合、MyCallback(Param1, *)のように、最後のパラメータとしてアスタリスクを指定する必要があります。

The callback can return a non-zero integer to prevent the script from exiting (with some rare exceptions) and calling more callbacks. Otherwise, the script exits after all registered callbacks are called.

AddRemove

型:整数

省略されたときは、初期値は 1 です。それ以外の場合は、次のいずれかの番号を指定してください:

備考

Any number of callbacks can be registered. A callback usually should not call ExitApp; if it does, the script terminates immediately.

The callbacks are called when the script exits by any means (except when it is killed by something like "End Task"). また、#SingleInstanceReloadで前のインスタンスの終了を要求されたときにも呼び出されます。

スクリプトは、OnMessage(0x0011, On_WM_QUERYENDSESSION)によって、システムのシャットダウンやログオフを検出し、オプションで中止することができます(動作するスクリプトについては、OnMessage の例 #2を参照)。

OnExitスレッド#MaxThreadsに従いません(必要なときに常に起動します)。また、実行中は、ホットキーカスタムメニュー項目時間指定サブルーチンなど、いかなるスレッドによっても中断することができない。ただし、トレイメニューやメインメニューでユーザーが「終了」を選択した場合や、Reload#SingleInstanceの結果、スクリプトの終了を求められた場合は、中断されます(スクリプトも終了します)。Because of this, a callback should be designed to finish quickly unless the user is aware of what it is doing.

OnExitスレッドがランタイムエラーなどの失敗条件に遭遇した場合、スクリプトは終了します。

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

Whenever an exit attempt is made, each callback starts off fresh with the default values for settings such as SendMode. これらのデフォルトは、スクリプト起動時に変更することができます。

Exit Reasons

Reason 説明
Logoff ユーザーがログオフしている。
Shutdown シャットダウン機能などで、システムがシャットダウンまたは再起動中です。
Close

スクリプトにWM_CLOSEまたはWM_QUITメッセージが送信されたか、クリティカルエラーが発生したか、または他の方法で終了しています。いずれも異常ですが、WM_CLOSEは、スクリプトのメインウィンドウでWinCloseが使用されたことが原因かもしれません。スクリプトを終了させずにウィンドウを閉じる(隠す)には、WinHideを使用します。

クリティカルエラーやメインウィンドウの破壊によってスクリプトが終了する場合、OnExitスレッドが完了した後に無条件で終了します。

メインウィンドウが破壊されている場合、存在しても表示できないことがあります。この状態は、OnMessageでWM_DESTROYメッセージを監視することで検出することができます。

Error 永続的でないスクリプトでランタイムエラーが発生しました。ランタイムエラーの例として、Run/RunWaitが指定されたプログラムやドキュメントを起動できないことが挙げられます。
Menu The user selected Exit from the main window's menu or from the standard tray menu.
Exit Exit or ExitApp was used (includes custom menu items).
Reload スクリプトは、リロード機能またはメニュー項目によって再読み込みされています。
Single スクリプトは、#SingleInstanceの結果、自分自身の新しいインスタンスに置き換えられています。

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

スクリプトを終了する前に、ユーザーに問いかけます。To test this example, right-click the tray icon and click Exit.

Persistent; スクリプトが自動的に終了するのを防ぎます。
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  ; Callbacks must return non-zero to avoid exit.
    }
    ; Do not call ExitApp -- that would prevent other callbacks from being called.
}

終了時に呼び出されるメソッドを登録します。

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()
        */
    }
}