SetTimer

指定した時間間隔で、自動的に繰り返し関数を呼び出します。

SetTimer Function, Period, Priority

パラメータ

機能

型:機能オブジェクト

呼び出す関数オブジェクト。

関数オブジェクトへの参照は、スクリプトのタイマーリストに保持され、タイマーが削除されない限り解放されない。これはランワンスタイマーでは自動的に行われますが、SetTimerをPeriodを0にして呼び出すことによっても行うことができます。

Functionが省略された場合、SetTimerは現在のスレッドを起動したタイマーを操作します(もしあれば)。例えば、SetTimer , 0をタイマー関数内で使用すると、タイマーを削除するためにマークすることができ、SetTimer , 1000は現在のタイマーの周期を更新します。

注:空の変数や空の値をもたらす式を渡すとエラーとみなされます。このパラメータには、空でない値を与えるか、完全に省略する必要があります。

Period

型:整数

If omitted and the timer does not exist, it will be created with a period of 250. If omitted and the timer already exists, it will be reset at its former period unless Priority is specified. Otherwise, the absolute value of this parameter is used as the approximate number of milliseconds that must pass before the timer is executed. The timer will be automatically reset. 自動で繰り返す設定や、1回だけ実行する設定も可能です:

The absolute value of Period must be no larger than 4294967295 ms (49.7 days).

Priority

型:整数

省略された場合、デフォルトは 0 です。Otherwise, specify an integer between -2147483648 and 2147483647 (or an expression) to indicate this timer's thread priority. 詳しくは「スレッド」をご覧ください。

既存のタイマーの優先順位を変更する場合、他の方法で影響を与えることなく、Periodを省略します。

備考

タイマーは非同期で実行されるため、スクリプトがウィンドウを待っていたり、ダイアログを表示していたり、他のタスクで忙しかったりしても、指定した頻度(間隔)で実行されるため便利です。例えば、ユーザーがアイドル状態になったときに何らかのアクションを起こす(A_TimeIdleで反映される)、不要なウィンドウが表示されたらすぐに閉じるなど、さまざまな使い方があります。

タイマーは、スクリプトが同時に複数のタスクを実行しているように錯覚させるかもしれませんが、そうではありません。その代わり、タイムドファンクションは他のスレッドと同じように扱われます:ホットキーのサブルーチンなど、他のスレッドに割り込んだり、割り込まれたりする可能性があります。詳しくは「スレッド」をご覧ください。

Whenever a timer is created or updated with a new period, its function will not be called right away; its time period must expire first. If you wish the timer's first execution to be immediate, call the timer's function directly (however, this will not start a new thread like the timer itself does; so settings such as SendMode will not start off at their defaults).

Reset: If SetTimer is used on an existing timer, the timer is reset (unless Priority is specified and Period is omitted); in other words, the entirety of its period must elapse before its function will be called again.

Timer precision: Due to the granularity of the OS's time-keeping system, Period is typically rounded up to the nearest multiple of 10 or 15.6 milliseconds (depending on the type of hardware and drivers installed). DllCall+timeBeginPeriod+Sleepのように、Loop+Sleepでより短い遅延を達成することもできる。

Reliability: A timer might not be able to run at the expected time under the following conditions:

  1. 他のアプリケーションがCPUに大きな負荷をかけている。
  2. 再びタイマー期間が終了しても、タイマーの機能は作動しています。
  3. 他に競合するタイマーが多すぎる。
  4. タイマーが他のスレッド、すなわち他のタイマー付き関数、ホットキーサブルーチン、またはカスタムメニュー項目によって中断された(これはCriticalによって回避することができます)。このとき、割り込みスレッドが終了するまでに長い時間がかかると、その間、割り込みタイマーは事実上無効となります。ただし、他のタイマーは、最初のタイマーを中断したスレッドに割り込んで実行し続ける。
  5. The script is uninterruptible as a result of Critical or Thread Interrupt/Priority. この間、タイマーは作動しません。その後、スクリプトが再び中断可能になると、期限を過ぎたタイマーはできるだけ早く1回実行し、その後通常のスケジュールを再開します。

Although timers will operate when the script is suspended, they will not run if the current thread has Thread NoTimers in effect or whenever any thread is paused. In addition, they do not operate when the user is navigating through one of the script's menus (such as the tray icon menu or a menu bar).

タイマーは、スクリプトの現在の活動を一時的に中断することで動作するため、長い中断が望ましくない場合は、タイマーの機能を短く保つ(すぐに終了するようにする)必要があります。

Other remarks: A temporary timer might often be disabled by its own function (see examples at the bottom of this page).

タイマーから関数が呼び出されると、SendModeなどの設定は常にデフォルト値で新しくスタートします。これらのデフォルトは、スクリプト起動時に変更することができます。

If hotkey response time is crucial (such as in games) and the script contains any timers whose functions take longer than about 5 ms to execute, use the following function to avoid any chance of a 15 ms delay. このような遅延は、タイマースレッドが中断しない期間にある瞬間にホットキーが押された場合に発生します:

Thread "Interrupt", 0  ; Make all threads always-interruptible.

タイマーが機能実行中に無効化された場合、その機能は完了するまで継続されます。

KeyHistory機能は、タイマーの存在と現在有効なタイマーの数を表示します。

Threads, Thread (function), Critical, Function Objects

不要なウィンドウが表示されたら、その都度閉じる。

SetTimer CloseMailWarnings, 250

CloseMailWarnings()
{
    WinClose "Microsoft Outlook", "A timeout occured while communicating"
    WinClose "Microsoft Outlook", "A connection to the server could not be established"
}

特定のウィンドウが表示されるのを待ち、ユーザーに警告を発する。

SetTimer Alert1, 500

Alert1()
{
    if not WinExist("Video Conversion", "Process Complete")
        return
    ; それ以外の場合。
    SetTimer , 0  ; i.e. the timer turns itself off here.
    MsgBox "The video conversion is finished."
}

ホットキーの単押し、二重押し、三重押しを検出します。これにより、ホットキーは押す回数によって異なる操作を行うことができます。

#c::
KeyWinC(ThisHotkey)  ; This is a named function hotkey.
{
    static winc_presses := 0
    if winc_presses > 0 ; SetTimer already started, so we log the keypress instead.
    {
        winc_presses += 1
        return
    }
    ; Otherwise, this is the first press of a new series. Set count to 1 and start
    ; the timer:
    winc_presses := 1
    SetTimer After400, -400 ; Wait for more presses within a 400 millisecond window.

After400()  ; This is a nested function.
    {
        if winc_presses = 1 ; The key was pressed once.
        {
            Run "m:\"  ; Open a folder.
        }
        else if winc_presses = 2 ; The key was pressed twice.
        {
            Run "m:\multimedia"  ; Open a different folder.
        }
        else if winc_presses > 2
        {
            MsgBox "Three or more clicks detected."
        }
        ; Regardless of which action above was triggered, reset the count to
        ; prepare for the next series of presses:
        winc_presses := 0
    }
}

Uses a method as the timer function.

counter := SecondCounter()
counter.Start
Sleep 5000
counter.Stop
Sleep 2000

; An example class for counting the seconds...
class SecondCounter {
    __New() {
        this.interval := 1000
        this.count := 0
        ; Tick() has an implicit parameter "this" which is a reference to
        ; the object, so we need to create a function which encapsulates
        ; "this" and the method to call:
        this.timer := ObjBindMethod(this, "Tick")
    }
    Start() {
        SetTimer this.timer, this.interval
        ToolTip "Counter started"
    }
    Stop() {
        ; To turn off the timer, we must pass the same object as before:
        SetTimer this.timer, 0
        ToolTip "Counter stopped at " this.count
    }
    ; In this example, the timer calls this method:
    Tick() {
        ToolTip ++this.count
    }
}

上記の例に関連するヒント: