# Function Reference

# _WinAPI_SetTimer

Creates a timer with the specified time-out value

#include <WinAPISysWin.au3>
_WinAPI_SetTimer ( $hWnd, $iTimerID, $iElapse, $pTimerFunc )

## Parameters

$hWnd | Handle to the window to be associated with the timer. This window must be owned by the calling process. If a 0 value for $hWnd is passed in along with an $iTimerID of an existing timer, that timer will be replaced in the same way that an existing non-zero $hWnd timer will be.
$iTimerID | The timer identifier. If the $hWnd parameter is 0, and the $iTimerID does not match an existing timer then it is ignored and a new timer ID is generated. If the $hWnd parameter is not 0 and the window specified by $hWnd already has a timer with the value $iTimerID, then the existing timer is replaced by the new timer. When _WinAPI_SetTimer() replaces a timer, the timer is reset. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored. If the call is not intended to replace an existing timer, $iTimerID should be 0 if the $hWnd is 0.
$iElapse | The time-out value, in milliseconds.
$pTimerFunc | The address of a callback function to be notified when the time-out value elapses. If this parameter is 0, the system posts a WM_TIMER message to the application queue. (See MSDN for more information)

## Return Value

Success: | The timer identifier. An application can pass this value to the _WinAPI_KillTimer() function to destroy the timer.
Failure: | 0.

## Remarks

The timer identifier, $iTimerID, is specific to the associated window. Another window can have its own timer
which has the same identifier as a timer owned by another window. The timers are distinct.

## See Also

Search SetTimer in MSDN Library.

## Example

### Example 1

    #include <Misc.au3>
    #include <WinAPISysWin.au3>

    Opt('TrayAutoPause', 0)

    Example()

    Func Example()
        Local $hTimerProc = DllCallbackRegister('_TimerProc', 'none', 'hwnd;uint;uint_ptr;dword')

        Local $iTimerID = _WinAPI_SetTimer(0, 0, 1000, DllCallbackGetPtr($hTimerProc))

        ; close script by pressing {ESC}
        Do
            Sleep(100)
        Until _IsPressed('1B')

        _WinAPI_KillTimer(0, $iTimerID)
        DllCallbackFree($hTimerProc)

    EndFunc   ;==>Example

    Func _TimerProc($hWnd, $iMsg, $iTimerID, $iTime)
        #forceref $hWnd, $iMsg, $iTimerID, $iTime
        Local Static $iCount = 1

        ToolTip("$iCount = " & $iCount, Default, Default, "Press {ESC} to close the script")
        $iCount += 1
    EndFunc   ;==>_TimerProc

### Example 2 - $pTimerFunc = 0 to use a $WM_TIMER callback

    #include <GUIConstantsEx.au3>
    #include <Misc.au3>
    #include <WinAPISysWin.au3>
    #include <WindowsNotifsConstants.au3>

    Opt('TrayAutoPause', 0)

    Example()

    Func Example()
        GUIRegisterMsg($WM_TIMER, "WM_TIMER")

        Local $hGUI = GUICreate("_WinAPI_SetTimer[2] - v(" & @AutoItVersion & ")")
        Local $iTimerID = 999
        $iTimerID = _WinAPI_SetTimer($hGUI, $iTimerID, 1000, 0)

        ; close script by pressing {ESC]
        Do
            Sleep(100)
        Until _IsPressed('1B')

        _WinAPI_KillTimer($hGUI, $iTimerID)

        GUIRegisterMsg($WM_TIMER, "")
    EndFunc   ;==>Example

    Func WM_TIMER($hWnd, $iMsg, $iwParam, $ilParam)
        #forceref $hWnd, $iMsg, $iwParam, $ilParam
        Local Static $iCount = 1

        ToolTip("$iCount = " & $iCount, Default, Default, "Press {ESC} to close the script")
        $iCount += 1

        Return $GUI_RUNDEFMSG
    EndFunc   ;==>WM_TIMER