Freeonterminate members in Delphi's TThread

Source: Internet
Author: User

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

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.