Delphi comes with a timer control that is easy to use, but its OnTimer event is raised in the main thread.
If you execute more time-consuming code in the event, it causes the main interface to feign death. Therefore, it is necessary to implement a timer for a thread.
Tthreadtimer is based on Tsimplethread inheritance.
This example source code download
UnitUthreadtimer;InterfaceusesUsimplethread;typeTthreadtimer=class;//Advance declaration Tthreadtimer is a classTonthreadtimer=procedure(Sender:tthreadtimer) of Object; //Here you can refer to Tthreadtimer, which avoids writing Sender as TObject; //Why write this sender, mainly to distinguish who caused the event, and sender can take parameters //convenient for further useTthreadtimer=Class (Tsimplethread)Privatefinterval:cardinal; Fonthreadtimer:tonthreadtimer; procedureCounttimer; procedureDocounttimer; proceduresetinterval (val:cardinal); procedureSetonthreadtimer (Val:tonthreadtimer); procedureDoonthreadtimer;//Please learn this wording Public Constructor Create(Aallowactivex:boolean = False);//Aalowactivex is described in the parent class procedureStartthread;Override;//overloading the Startthread of the parent class PropertyInterval:cardinalReadFintervalWriteSetIntervaldefault +; //This default 1000 is for people to see and does not have a practical effect. //It is also necessary to specify finterval:=1000 in the Create event; //if the visual control is in the published block, this value is displayed in the Property edit box PropertyOnthreadtimer:tonthreadtimerReadFonthreadtimerWriteSetonthreadtimer; End;Implementation{Tthreadtimer}procedureTthreadtimer.counttimer;beginExeprocinthread (Docounttimer); //Place the Docounttimer into the thread to execute //This is the use of tsimplethread.End;ConstructorTthreadtimer.Create(Aallowactivex:boolean);begin inherited Create(Aallowactivex); Finterval:= +;//The default time interval is 1 secondsEnd;procedureTthreadtimer.docounttimer;begin ifWaitstop Then //This is a property of the parent class, indicating that the thread needs to stop now. exit; Sleepexceptstopped (Finterval); //The time specified by sleep, and responds immediately if an exit instruction is received halfway. //the parent class has the source code, can take a look if notWaitstop Then beginDoonthreadtimer;//raise time to event End; Counttimer; //execute the Docounttimer in the thread again; //The parent class has been designed so that it is simply called to implement this procedure in a thread without causing "recursion"End;procedureTthreadtimer.doonthreadtimer;begin ifAssigned (Fonthreadtimer) ThenFonthreadtimer (self); //This sentence is written as a process, seemingly verbose, but for the readability of the program is worthwhile. End;procedureTthreadtimer.startthread;begin inherited; Counttimer; //Start TimingEnd;procedureTthreadtimer.setinterval (val:cardinal);beginFinterval:=Val;End;procedureTthreadtimer.setonthreadtimer (val:tonthreadtimer);beginFonthreadtimer:=Val;End;End. Uthreadtimer.pas
Attached: Delphi Advanced Basic Skills Description
Http://www.cnblogs.com/lackey/p/5411389.html
Delphi Thread Timer (tthreadtimer)