How to end a thread in Delphi (this thread is executed on a timed basis) (scenario two)

Source: Internet
Author: User

The previous blog raised a question: how to end a timed loop execution of the thread, and give a solution, but there is a problem, in detail to refer to the previous blog.

Then go out and sprinkle a urine, suddenly there is an idea in the brain (it seems that work and thinking for a long time, go out, even if it is to sprinkle a urine, may be urine sparks, so often work and study is not the same as the efficiency of inspiration is not there to work hard, want to come out), Need to combine the previous blog: Knowledge about Freeonterminate

The first solution given above is:

  The thread (execute) executes a timed loop, and then lets Destroyathread set the terminated property, notifies the thread to end execution, frees the resource, and then notifies Destroyathread after the threads have finished executing, releasing the resources, The free method of the thread is then explicitly invoked by Destroyathread to release the thread

So combined with the role of Freeonterminate, review:

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.

To retrofit:

  First, the Execute method of the thread sets Freeonterminate to true and then executes its own timed loop, then Destroyathread sets the terminated property, notifies the thread to end execution, frees the resource, and then ends the execution , after releasing the resource, because Freeonterminate is set to True, do not notify Destroyathread, self-release after releasing the resources, end execution.

  Also, after setting the terminated property, you can exit directly because you do not need to explicitly release the thread in the Destroyathread. Destroyathread.

The same also does not need the thread to have canfree this notice other people to release its properties, so it can also simplify the design of threading class (in Object-oriented program design, a principle is: the class as small as possible, so those can not need the properties, methods do not define and use, the redundant properties and methods must be removed ...) )

So the new code can be like this

Execute method:

Procedure Tdosomethingthread.execute;var  i:integer;begin  inherited;  //canfree:    = False; It is no longer necessary to canfree such a property, it simplifies the design of the threading class   try    while not self.terminated do     begin      ........      {          here to write the code logic for the thread's specific things to do,         If there is still a loop to perform some of the tasks,         better be able to check the value of terminated in each cycle.           If true, ends the loop,         is to ensure that the terminated mark is constantly checked in a shorter event, and that the       }       can be withdrawn in time .......       sleep (5);       Application.processmessages;    end;  finally    //canfree: =    True; No longer need to canfree such a property, it issimplifies the design of threading classes     {the resources requested by the thread are released here ... Subsequent operations}  end;end;

Destroyathread Method:

Procedure Destroyathread (Testthread:tdosomethingthread);      Stdcall;var Itime:cardinal;begin if Assigned (testthread) then begin testthread.terminate; Here, set the terminated property of Testthread to True        {      You only need to set the terminated of the thread to true through the Terminate method,      to notify the thread that it is done,      There is no need to worry about thread release because threads release themselves    } end;end;

  

How to end a thread in Delphi (this thread is executed on a timed basis) (scenario two)

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.