http://www.autohotkey.com/board/topic/5 ... functions/
A:=FTimer(100, "t",1) ; 时间,函数,参数
A.Stop() ;暂停
A.Stop(1) ;结束
A.begin() ;继续
A.begin(200,"新") ;或修改时间及参数
A.begin("","新")
FTimer("t") ;结束所有调用"t"函数的定时器
Code: Select all
A:=FTimer(100, "t",1)
B:=FTimer(200, "t",2)
Loop
{
if a.Results.MaxIndex() > 10
{
A.begin(1000,"新") ;调用10次之后修改
Break
}
Sleep, 200
}
Sleep, 2000
FTimer("t")
MsgBox
t(a){
ToolTip,% a
return A_TickCount
}
Code: Select all
FTimer(Period,Func:="",params*){
Static h:=[]
if A_EventInfo and IsObject(f:=Object(A_EventInfo)) and !f.HasKey("End")
{
if f.Times
DllCall("KillTimer",UInt,0, PTR,f.ft)
f.Tick.Insert(A_TickCount)
f.Results.Insert(f.func.(f.params*))
if f.Times
h.Remove(&f,""),DllCall("GlobalFree", PTR,f.rc)
}
else
{
if !Func
{
b:=[]
for i,n in h.Clone()
(n.func=Period)?(n.Stop(1),b.Insert(n)):""
return b
}
if IsFunc(Func)
{
f:=New FTimer(h,Period,Func,params)
return f,h[&f]:=f
}
}
}
class FTimer {
__New(Total,Period,Func,params){
this.Total:=Total
this.func:=Func
this.Period:=Period
this.Priority:=Priority
this.params:=params
this.Times:=Period<0
this.Tick:=[A_TickCount]
this.Results:=[]
this.rc:=RegisterCallback("FTimer","F",4,&this)
this.ft:=DllCall("SetTimer", PTR,0, PTR,0, UInt,Abs(Period),PTR,this.rc,"PTR")
}
Stop(d:=""){
this.Kill:=DllCall("KillTimer",UInt,0, PTR,this.ft)
d?this.__Delete():""
}
begin(Period:="",params*){
DllCall("KillTimer",UInt,0, PTR,this.ft)
params.MaxIndex() ? this.params:=params :""
Period ? this.Times:=(this.Period:=Period)<0 :""
this.Kill:=""
this.ft:=DllCall("SetTimer", PTR,0, PTR,0, UInt,Abs(this.Period),PTR,this.rc)
}
__Delete(){
if !this.HasKey("End")
this.Kill?"":DllCall("KillTimer",UInt,0, PTR,this.ft)
,this.End:=DllCall("GlobalFree", PTR,this.rc)
,this.Total.Remove(&this,"")
}
}