SetTimer

提供: AutoHotkey Wiki
移動: 案内検索

実行制御 | GUI表示 | 演算・変数 | メモリ・DLL操作 | 文字列操作 | キーボード | マウス | シェル | ウィンドウ | ウィンドウグループ
ステータスバー | コントロール | サウンド | ファイル | INIファイル | レジストリ | 環境変数 | AutoHotkey | その他 | 設定関係 | オブジェクト

SetTimer[編集]

サブルーチンを指定間隔で実行する。

SetTimer, Label [, Period|On|Off, Priority]

Parameters[編集]

引数名 説明
Label ラベル名。ホットキーラベルなどでも可能。
%LabelName%のような変数参照でもよい。
Period|On|Off 間隔 数値を指定すると、実行間隔をミリ秒で設定できる。上限は4294967295ミリ秒(約49.7日)である。
タイマーは自動的に有効になる。
負の値を指定した場合、その絶対値の時間が経過後に、一度だけサブルーチンが実行される。
On Offにしたタイマーを再度有効にする。
タイマーが新規作成される場合、間隔は250に設定される。
過去にタイマー登録されたことがある場合は、そのときと同じ間隔に設定される。(負の値が指定されていた場合も)
Off タイマーを停止する。
Onと同じ。ただし、Priorityが設定されている場合、経過時間のリセットは行われない。
Priority スレッドの割り込み優先度を指定。
数値の指定には を用いることが可能。

Remarks[編集]

タイマーを使用すれば、定期的に処理を実行したり、何かを監視したりすることができる。

タイマーを設定しても常駐にはならないので、ホットキーを登録しないスクリプトを常駐させたい場合#Persistent指令を記述する必要がある。

タイマーは、設定されたあと指定時間が経過してから初めて実行される。
Onにしたり間隔を設定したりすると、前回実行からの経過時間がリセットされ、現在の時間から指定時間経過後に次の実行が行われる。

タイマーは、前回のスレッドが開始されたときから指定時間経過後に次が実行される。
ただし、同じタイマースレッドが重複して実行されることはない。
同じタイマーのタイマースレッドが実行中に、次の時間が来た場合、既存のタイマースレッドの終了を待って即座に次のタイマースレッドが開始される。

Suspendされている間もタイマーは動き続ける。
Pauseされると全てのタイマーは停止する。

OSの制限により、NT系では10ミリ秒、9x系では55ミリ秒より短い間隔では実行できない。
これより短い間隔を指定した場合は、最小間隔に設定される。

タイマーやホットキーが確実に指定通りに実行されるようにするには、SetBatchLinesによる処理速度やThreadによるスレッド割り込みの設定などを調節する必要がある。

Related[編集]

Gosub, Return, スレッド, Menu, #Persistent

Example(s)[編集]

; Example #1: Close unwanted windows whenever they appear:
SetTimer, CloseMailWarnings, 250
Return
CloseMailWarnings:
WinClose, Microsoft Outlook, A timeout occured while communicating
WinClose, Microsoft Outlook, A connection to the server could not be established
Return
; Example #2: Wait for a certain window to appear and then alert the user:
SetTimer, Alert1, 500
Return
Alert1:
IfWinNotExist, Video Conversion, Process Complete
  Return
; Otherwise:
SetTimer, Alert1, Off		; i.e. the timer turns itself off here.
SplashTextOn, , , The video conversion is finished.
Sleep, 3000
SplashTextOff
Return
; Example #3: Detection of single, double, and triple-presses of a hotkey. This
; allows a hotkey to perform a different operation depending on how many times
; you press it:
#c::
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, KeyWinC, 400		; Wait for more presses within a 400 millisecond window.
Return
KeyWinC:
SetTimer, KeyWinC, off
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
Return