Delphi Thread Timer (tthreadtimer)

Source: Internet
Author: User
Tags setinterval

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.