How to respond to user operation while data processing in the case of data processing? Windows95 / 98 and Windows NT as multi-threaded multi-task operating systems, its scheduling unit is a thread, that is, threading is the basic unit of the system allocates the processor time resource, so we can use threads to effectively respond user operation while processing large amounts of data.
As an excellent development platform, Delphi provides strong support for developing multi-threaded applications, you can directly utilize the 32API interface function CreateThread provided by the 32-bit Windows environment, and you can use the BeginThread function provided in Delphi. In the following example, it is using the TTHREAD class provided by Delphi.
One. The basic method is as follows:
1. A new class is derived from the TTHREAD class. (Create Tsortthread class)
2. Define the Create method for the new class.
3. Define the EXECUTE method of the new class and insert the code executed when the thread is running in the Execute method.
4. Create an instance of the category method.
two. Example of detailed code and description:
First, create a new unit, saved as mysort.pas. In this unit, we created a TSORTTHREAD class, which inherited from the TTHREAD class, so when we create this class in the program, it is created. A new thread.
Next, a Sort method is defined in this class to sort an array while the TsortThread class beyond the construction method of the TTHREAD class CREATE and EXECUTE, in the Execute method, the SORT method for sorting the array is called. The specific code is as follows:
UnitMysort;
Interface
UsesClasses; // TTHREAD class is defined in Classes.
Type
Psortarray = TsortArray;
TsortArray = array. [0..maxintdivsize
Of (Integer) -1] OFINTEGER;
{The TsortThread class is defined here.
Tsortthread = Class (TTHREAD)
Private
{The following private variables are defined in the TsortThread class}
FSORTARRAY: PSORTARRAY;
Fsize: integer;
FA, FB, FI, FJ: Inteder;
Protected
{Class Tsortthread beyond the EXECUTE method of class TTHREAD}
Procedure execute; override;
{Class TsortThread adds an SORT method}
Procedure sort (VARA: Arrayofinteger);
public
{Class Tsortthread beyond the construction method of TTHREAD}
ConstructorCreate (VarsortArray: Arrayofinteger);
END;
IMPLEMENTATION
Constructortsortthread.create (VarsoteArray: arrayofinteger);
Begin
FSORTARRAY: = @ sORTARRAY;
Fsize: = high (sortarray) -low (sortarray) 1;
Freeon Terminate: = true;
InheritedCreate (FALSE);
END;
{When the thread starts, the Execute method will be called. }
Procedure tsortthread.execu
TE;
Begin
Sort (FSORTARRAY, FSIZE);
END;
{Sort by the bubbling method} procedure tsortthread.sort (VARA: Arrayofinteger);
VAR
I, J, T: Integer;
Begin
For i: = high (a) DOWNTO LOW (A) DO
For j: = low (a) to high (a) -1 do
IF A [J]> A [J 1] THEN
Begin
T: = a [j];
A [j]: = a [j 1];
A [J 1]: = T;
IF Terminated the EXIT;
END;
END;
end
Finally, add USESMYSORT at the user application's importation, add TQUicksortthread.create (SortArray) where the sort is executed, where SortArray is an actual array. This can be sorted by threading. During the sorting process, users do not have to wait until the end of the sort can be executed. This multi-thread real-time response user operation method is especially important in applications involving a large amount of data processing.