SetTimerF()
SetTimer functionality for functions!
Very many thanks to SKAN who researched and started this, and whos topic this was split from (see link above)*Note: I would not recommended SetTimerF periods less than 30-100ms since weird things could happen if it's called again before the user function is called.
/* SetTimerF: An attempt at replicating the entire SetTimer functionality for functions. Includes one-time and recurring timers. Thanks to SKAN for initial code and conceptual research. Modified by infogulch to copy SetTimer features On User Call: returns: true if success or false if failure p1: Function name p2: Delay (int)(0 to stop timer, positive to start, negative to run once) p3: (optional) Pointer to data (uInt)(must be persistent at another location) p4: (optional) Length of data (uInt) Note: p3 & p4 don't necessarily have to be a pointer and length , but they must be numerical (positive/negative/float) On Timer: (user) p3 and p4 are passed as the first and second params if the function accepts them ErrorLevel is set to the TickCount On Timer: (internal) p1: HWND (unused) p2: uMsg (unused) p3: idEvent (timer id) used internally ( as per http://msdn.microsoft.com/en-us/library/ms644907 ) p4: dwTime (tick count) Set ErrorLevel to this before user function call */ SetTimerF( p1, p2="", p3=0, p4=0 ) { Static tmrs, CBA if !CBA CBA := RegisterCallback( A_ThisFunc, "", 4 ) If IsFunc( p1 ) { if RegExMatch(tmrs, "(?i)^(?<pre>.*)(?<=^|;)(?<tmr>\d+)," p1 ",[^;]*;(?<post>.*)$", _) ret := DllCall( "KillTimer", UInt,0, UInt, _tmr ), tmrs := _pre _post if (p2 = 0) return ret return !!tmr := DllCall( "SetTimer", UInt,0, UInt,0, UInt,p2 ? Abs(p2) : (p2 := 250), UInt,CBA ) , tmrs .= tmr "," p1 "," p2 "," (p3+=0) "," (p4+=0) ";" } RegExMatch(tmrs, "^(?<pre>.*)(?<=^|;)" p3 ",(?<func>[\da-zA-Z@#$_]+),(?<delay>-?\d+),(?<ptr>\d*),(?<len>\d*);(?<post>.*)$", _) if (_delay < 0) DllCall( "KillTimer", UInt,0, UInt, p3 ), tmrs := _pre _post ErrorLevel := p4, %_func%( _ptr, _len ) }
Note there is an AHK_L version a couple posts below.
All Versions: http://db.tt/A0Woi8R