Multi-threaded programming under DELPHI (1)
We know that win95 or winNT are both "multi-threaded" operating systems. In DELPHI 2.0, we can make full use of this feature and write "multi-threaded" applications.
For those who wrote programs under DOS or 16-bit windows in the past, "multi-threading" is still unfamiliar, but just as we transitioned from single task under DOS to multi-tasking under Windows 3.1, now we must make the transition again To the field of "multi-threading", after all, the computer age is constantly developing. However, fortunately, multi-threaded programming under DELPHI2.0 does not require us to learn the huge WIN32API functions. We can use the standard multi-threaded class TThread under DELPHI to complete our work.
TThread is an abstract class, that is to say, there is no need to declare variables based on TThread (and variables declared based on TThread are completely useless). What we have to do is to use TThread as a base class and generate it in the form of inheritance. subclass. In fact, it is very easy to write multi-threaded applications based on TThread.
The following is a basic multi-threaded class generated by inheriting TThread.
QuerThrd. Pas
unitQuerThrd;
interface
uses
Classes,DBTables;
type
TQueryThreadΚclass(TThread)
PRivate
fQuery:tQuery;
protected
procedureExecute;override;
public
constructorCreate(Suspended: Boolean; Query: TQuery);
end;
implementation
constructor
TQueryThread. Create(Suspended: Boolean; Query: TQuery);
begin
inheritedCreate(Suspended);
fQuery: ΚQuery;
FreeOnTerminate:ΚTrue;
end;
procedureTQueryThread. Execute;
begin
fQuery. Open;
end;
end.
In the simple example above, we constructed a TQuery-Thread subclass of TThread to execute database queries in the background. In the Create function of this class, two parameters Suspended and Query are passed, where Suspended is used to control the running of the thread. If Suspend is true, the thread of the TQueryThread class will be suspended immediately after it is established until the Resume method is run. The thread will continue to execute. The Query parameter is used to accept an existing Query control (the real Query control in the form) to make it run in a multi-threaded situation. Execute is the most important process. It is the execution part of the class TQueryThread. All statements that need to be run in this multi-threaded class must be written in this process.
In fact, when constructing your own multi-threaded class, you do not need to enter all these codes. Select the new option under DELPHI's File menu, then select the "TThreadObject" project, and DELPHI will construct the basic program module for you. Then we can make corresponding modifications as needed.
Process execution:
Suppose we have created a form FORM1, which contains the query control Query1 we are going to use. Then we add the QuerThrd unit written above to the USES part of the unit.
procedureTForm1. Button1Click(Sender: TObject);
begin
{Create a running process}
TQueryThread. Create(False,Query1);
end;
If this process is executed, the query control Query1 in the form will automatically run the query in a multi-threaded environment. Note that there is only Create but not Free in the TQueryThread class. After dynamically creating a class and then forgetting to delete it, it is one of the mistakes we often make. However, since we have specified FreeOnTerminate (delete after running) as true here, when the statement in Execute is executed After completion, the memory control occupied by the TQueryThread class will be automatically released.
However, there is another issue worthy of our attention. Since multiple threads can run at the same time, we must also solve the synchronization problem. If there is no correlation between several multi-threaded programs, then there will be no correlation between them. any conflict. But in fact, several multi-threaded database applications may be running at the same time. Since the same database resources need to be shared, we also need to add a Tsession control to Query1.
In fact, although we may not have personally used the Session control, in fact, DELPHI will automatically create a temporary Session control during all database accesses, and dynamically delete it after use. In normal database programming, we do not need to do it ourselves, but in the case of multi-threaded execution of the database, in order not to conflict with each other, we must customize our own Session control for each database access. This step is very simple. We only need to add a Session control to the form, then write an arbitrary name to its property "Sessionname", and then write the same name in the "Sessionname" of Query1. This way our database program is safe.
Another type of synchronization problem that needs to be solved is those programs that operate on VCL resources. There are many such programs, but fortunately the solution is also very simple.
We can look at a program like this:
unitBncThrd;
interface
uses
WinProcs, Classes, Graphics, ExtCtrls;
type
TBounceThreadΚclass(TThread)
private
FShape: TShape;
FXSpeed: Integer;
FYSpeed: Integer;
procedureMoveShape;
protected
procedureExecute;override;
public
constructorCreate(Suspended: Boolean; Shape: TShape; XSpeed, YSpeed: Integer);
propertyShape: TShapereadFShape;
end;
implementation
procedureTBouad. MoveShape;
var
MaxHeight, MaxWidth: Integer;
begin
withFShapedo
begin
Left: ΚLeft+FXSpeed;
Top: ΚTop+FYSpeed;
if(LeftΙ0)or
(Left+WidthΛParent.Width)then
FXSpeed: ΚFXSpeed*-1;
if(TopΙ0)or
(Top+HeightΛParent.Height)then
FYSpeed: ΚFYSpeed*-1;
Author's Blog: http://blog.csdn.net/zou5655/