Teach you how to use thread programming in Delphi

zhaozj2021-02-16  56

Why use a thread, simple, save some newcomers like me, don't understand, when you have finished a program, for example, if you read the file, if you read the file, if your program If you do this through the thread, you move this program window that is reading the file, what is the result, "dead screen" is your program, you can't move, you can't continue to do other things, huh, if you use threads, What problems are solved, nonsense, learn to learn the following article.

First of all, it is of course to open your delphi 6. Tap the file in the menu bar - New-Other, pop up a tag window, select the New tab, then find thread object, it is it, double click it, you will play, pop up a class named window, Enter mythread, of course, the name can be kept by you. At this time, the program automatically creates a unit, I am Unit2 here, now let's see Unit, the code is as follows:

Unit unit2;

Interface

Uses classes;

Type mythread = class (tthread) private {private declarations} protected procedure execute; override;

IMPLEMENTATION

{Important: Methods and Properties Of Objects in vcl or clox can notle be used in a method caled sales synchronize, for example,

Synchronize (UpdateCaption);

And UpdateCaption Could Look Like,

Procedure mythread.updatecaption; begin form1.caption: = 'Updated in a thread'; end;}

{mythread}

Procedure mythread.execute; begin {place thread code heren;

End.

Among them, you pay attention to find procedure mythread.execute; you should find it, even I saw it, this is what you just built, then next, what we have to do is to join the code executed in the background, code to add There? No, of course, you are adding in Begin // here is an end; if you want to call the control on Unit1, you can join Unit1 in the USES above Unit2, remember, in Unit1 Implementation increases Uses Unit2, so you can quote the thread in Unit1. The reference method is very simple, that is, that is, it is, good, don't sell it, just mythread.create (false) ;. OK this is the thread in delphi, huh, huh.

I have just learned how Delphi is wrong, welcome to evaluate the appraisal, my contact mail box NetBoy@cnpick.com Thank you!

转载请注明原文地址:https://www.9cbs.com/read-27710.html

New Post(0)