In the case of large amount of data processing, how to respond to user operations while processing? WINDOWS95/98 and WindowsNT as multi-threaded multitasking operating system, its dispatch unit is the thread, that is, the thread is the system allocates the processor Time resource the basic unit, so we can use the thread implementation to handle the large amount of data while responding to user action.
As an excellent development platform, Delphi provides strong support for developing multi-threaded applications, and can directly take advantage of the Win32API interface functions provided by the 32-bit Windows environment CreateThread You can also use the Beginthread function provided in Delphi. In the following example, it is using the TThread class provided by Delphi.
A The basic approach is as follows:
1. Derive a new class from the TThread class. (Create Tsortthread Class)
2. Define the Create method for the new class.
3. Defines the Execute method for the new class and inserts the code executed by the thread runtime in the Execute method.
4. The reference class method creates an instance.
Two Examples of detailed code and description:
First, create a new cell and save it as Mysort.pas. In this unit, we created a Tsortthread class that inherits from the TThread class, so when we create an instance of the class in the program, we create a new thread.
Next, define a sort method in the class to sort the array, while the Tsortthread class transcends the constructor for the TThread class create and execute, and in the Execute method, calls the sort method of the array. The specific code is as follows:
The
unitmysort
Interface
Usesclasses;//tthread class is defined in classes.
Type
Psortarray=tsortarray;
Tsortarray=array. [0..maxintdivsize
of (Integer) -1]ofinteger;
{The Tsortthread class}
Tsortthread=class (tthread)
Private
{defined here defines the following private variable}
Fsortarray in the Tsortthread class: Psortarray;
Fsize:integer;
Fa,fb,fi,fj:integer; The
Protected
{class Tsortthread Beyond the class TThread Execute method}
procedure execute;override;
{Class Tsortthread added a sort method}
Procedure Sort (Vara:arrayofinteger);
Public
{class Tsortthread beyond the constructor for class TThread}
Constructorcreate (Varsortarray:arrayofinteger);
End;
Implementation
Constructortsortthread.create (Varsortarray:arrayofinteger);
Begin
Fsortarray:=@ Sortarray;
Fsize:=high (Sortarray)-low (Sortarray) +1;
Freeon terminate:=true;
Inheritedcreate (False);
End;
{When the thread starts, the Execute method is invoked.
Procedure Tsortthread.execu
Te;
begin
Sort (Slice (fsortarray,fsize));
End {The bubble sorting is implemented below}
Procedure TSortthread.sort (Vara:arrayofinteger);
var
i,j,t:integer
Begin
for I:=high (a) Downto low (a) does
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 then Exit;
End;
End;
End
Finally, add the usesmysort at the implementation of the user application and join the Tquicksortthread.create (Sortarray) where the order is performed, where Sortarray is an actual array.
This allows you to sort by thread, in which the user does not have to wait for the sort end to perform other actions. This real-time response user action method with multithreading is particularly important in applications involving large amounts of data processing.