Class Create will have free;
But TThread (subclass) is special, many times we are not sure when the new thread is finished (that is, when to release);
If the thread finishes executing itself and knows to release it, TThread gives a Boolean attribute Freeonterminate, and if true, the thread will Self-release when it finishes executing.
First look at the following routines
Type Tmythread = Class (TThread) protected procedure Execute; override; End;procedure tmythread.execute;var i:integer;begin freeonterminate: = True; {This allows the thread to be freed after execution is complete} For I: = 0 to 500000 do begin Form1.Canvas.Lock; Form1.Canvas.TextOut (Ten, IntToStr (i)); Form1.Canvas.Unlock; End;end;procedure Tform1.button1click (sender:tobject); begin Tmythread.create (False); end;
The TThread class has an abstract method (Execute) and is therefore an abstract class, and the abstract class can only be inherited and inherited as Tmythread.
Inheriting TThread is basically implementing an abstract method of execute (writing our code inside), and when our tmythread is instantiated, the code in the Execute method is executed first.
We generally instantiate this as usual:
Procedure Tform1.button1click (sender:tobject); var mythread:tmythread;begin MyThread: = Tmythread.create ( False); end;
Because the MyThread variable is useless here (and the compiler has hints), it is better to write Tmythread.create (False) directly;
About Freeonterminate
In the case of the TThread class, there should be this sentence: freeonterminate: = True; (The original is missing, the code is added; But the animation is not added).
Say what it means first:
Class Create will have free;
But TThread (subclass) is special, many times we are not sure when the new thread is finished (that is, when to release);
If the thread finishes executing itself and knows to release it, TThread gives a Boolean attribute Freeonterminate, and if true, the thread will Self-release when it finishes executing.
Some books introduce that: the default value of Freeonterminate is True ( Error!), after implementation, should be False, at least in Delphi 2007 and 2009 is the case; Perhaps a previous version is not the same as it is now. You can avoid these problems by not considering which version it is, or whether the default value of Freeonterminate is true or false, to explicitly set it to true
Freeonterminate members in Delphi's TThread