Why use threads? To put it simply, so that some novices like me don’t understand, when you finish programming a program, for example, a program that reads files, if the file you read is large, your program will If it is not executed through threads, what will be the result when you move the program window that is reading the file? The "dead screen" means that your program cannot be dragged at all and cannot continue to do other things. Haha, if you use threads, All problems have been solved, so stop talking nonsense and read the following article if you want to learn.
First of all, of course, open your delphi 6, click File-New-Others in the menu bar, a label window will pop up, select the new label, then find the Thread Object, that's it, double-click it, a class naming window will pop up. Enter mythread, of course the name can be decided by yourself. At this time, the program automatically creates a unit. Here is unit2. Now let's look at the unit. The code is as follows:
unit Unit2;
interface
uses
Classes;
type
mythread = class(TThread)
PRivate
{Private declarations}
protected
procedure Execute; override;
end;
implementation
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using 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 here }
end;
end.
Among them, pay attention to find procedure mythread.execute;. You should have found it. Even I saw it. This is the thread you just created. Then what we have to do is to add the code for background execution. The code must be added in There? No, of course it will be added
begin
//This is where the program code is added
end;
If you want to call the control on unit1, you can just add unit1 to uses on unit2. Remember, add uses unit2 after implementation in unit1, so that you can reference the thread in unit1. The reference method is very simple. It’s simple, it’s just, it’s, it’s, okay, I’m not going to give it a second thought, it’s mythread.Create(false);. OK This is the thread in Delphi, haha.
I have just learned Delphi. If there is anything I say that is wrong, you are welcome to criticize and point out. My contact email is [email protected]. Thank you!