Multi-threaded program design under Delphi (1)

zhaozj2021-02-17  54

Multi-threaded program design under Delphi (1)

We know that Win95 or Winnt is a "multi-thread" operating system. In Delphi 2.0, we can make full use of this feature to write "multithreaded" applications.

For those who write programs under DOS or 16-bit Windows, "Multi-Thread" is still unfamiliar, but as before we transition from the single task under DOS to Windows3.1's multitasking, now we must transition To the "multi-thread" area, after all, the computer era is constantly evolving. However, fortunately, in Delphi2.0, multithreaded programming does not require us to learn the huge Win32API function, we can use the standard multi-threaded TTHREAD under Delphi to complete our work.

TTHREAD is an Abstract (abstract) class, that is, do not need to declare the variable according to TTHRead (and is also completely useless according to TTHREAD declaration), and we have to do it to generate TTHREAD as a base class, generated in inheritance Subclass. In fact, it is very easy to write multi-threaded applications based on TTHREAD.

Below is a basic inheritance TTHREAD generated multi-threaded class.

Querthrd. PAS

UnitQuerthrd;

Interface

Uses

Classes, DBTABLES;

Type

TQuerythreadκClass (TTHREAD)

Private

FQUERY: TQUERY;

protected

Procedureexecute; Override;

public

ConstructorCreate (SUSPENDED: BOOLERY; Query: TQuery);

END;

IMPLEMENTATION

Constructor

Tquerythread. Create (SUSPENDED: BOOLERY; Query: tQuery);

Begin

InheritedCreate (SUSPENDED);

κQuery;

FreeOnterminate: κtrue;

END;

ProceduretQuerythread. EXECUTE;

Begin

FQuery. Open;

END;

End.

In this simple example, we constructed a subclass of TTHREAD tQuerythread for 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 operation. If SUSPEND is true, the thread of the TQuerythread class will be immediately hanging immediately, and the resume method is running. The thread will continue to execute, Query parameters are used to accept an existing Query control (real QUERY control in the form) and run it in a multi-thread. Execute is the most important process, which is the execution section of class TQuerythread, all statements that need to be run in this multi-thread class must be written in this process.

In fact, when constructing your own multi-thread class, you don't need to enter all of these code, select the New option under Delphi's File menu, select the "TTHREADOBJECT" project, and Delphi will construct a basic program module for you. Then we can do corresponding modifications as needed. Performance of the process:

Suppose we have established a form FORM1, and there is a query control query11 we will use. Then we join the querthrd unit written above in the USES section of the unit.

Proceduretform1. Button1Click (Sender: TOBJECT);

Begin

{Establish a running process}

Tquerythread. Create (false, query1);

END;

If this process is executed, the query control Query1 in the form will automatically run in a multi-threaded environment. Note that only CREATE in the tQuerythread class does not free, and the dynamic establishment class will forget to delete one of our frequent mistakes, but here we specify that freeOnterminate is true, so when the statement in Execute is executed After that, the memory controls occupied by the TQuerythread class will be automatically released.

However, there is still a question worth noting that because there are multiple threads at the same time, we must also solve the problem of synchronization, if there is no association between several multi-threaded programs, then there will be no Any conflict. But in fact, you may run several multi-threaded database applications, we also need to add a TSESSION control for Query1 because we need to share the same database resources.

In fact, although we may have not to use the session control, but in fact, Delphi will automatically establish a temporary session control while all database access, and then dynamically delete it. When the usual database is programmed, it is not necessary to operate in person, but in the case of multiple threads performed by the database, in order not to conflict, we must customize your own session control for each database. This step is very simple, we only need to add a session control in the form, then write an arbitrary name to its attribute "sessionname" and write an identical name in Query1's "sessionname". This happens to be safe.

Another category needs to solve synchronization problems is those processes that operate on VCL resources. There are many programs that are well solved. It is also very simple.

We can see the following programs:

UNITBNCTHRD;

Interface

Uses

WinProcs, Classes, Graphics, ExtCtrls

Type

TBounceThread (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: let fxspeed;

Top: κtop fyspeed;

IF (Leftι0) OR

(Left widthλParent.width) THEN

FXSPEED: κfxspeed * -1;

IF (TOPι0) OR

(TOP HeightλPar ent.Height) THENT

Fyspeed: κFyspeed * -1;

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

New Post(0)