DELPHI multithreading (TThread achieve class)

Before learning the implemented API, let's learn TThread under the category with the DELPHI.

First create a common project, and then create a new thread class File >> New >> Othre >> Delphi File> Thread Object, a name, DELPHI will automatically generate a unit, we just need to add a simple function inside the code, and in unit to be used in reference to examples.

To save space, is the main form an integrated unit TMyThread class, the class declaration in the form unit is also possible.

Example: Digital output form 0 to 500,000 in a worker thread.

Copy the code
. 1 Unit Unit1; 
 2 
 . 3 interface 
 . 4 
 . 5 uses 
 . 6 the Windows, the Messages, the SysUtils, Variants, the Classes, Graphics, Controls, Forms, 
 . 7 the Dialogs, StdCtrls; 
 . 8 
 . 9 type 
10 TMyThread = class (TThread) 
. 11 Private 
12 is {Private Declarations} 
13 is protected 
14 procedure execute; the override; {performed} 
15 the Run procedure; {declare a multiple process, the function code written here give execute call} 
16 End; 
. 17 TForm1 = class (a TForm) 
18 is btn1: the TButton; 
. 19 procedure btn1Click ( SENDER: TObject); 
20 is Private 
21 is} {Private Declarations 
22 is public 
23 is public Declarations {} 
24 End;
25 
53 is Form1.Canvas.TextOut (10,10, the IntToStr (I));
26 is 
27 
28 var 
29 the Form1: TForm1; 
30 
31 is 
32 Implementation 
33 is 
34 is $ {}. Dfm R & lt * 
35 
36 var 
37 [the MyThread: TMyThread; {declare a class object thread] 
38 is 
39 Procedure TMyThread.Execute; 
40 the begin 
41 is {Place Thread } here Wallpaper code 
42 is FreeOnTerminate: = True; {phrase plus thread runs out automatically annotated} 
43 is the run; 
44 is End; 
45 
46 is Procedure TMyThread.Run; 
47 var 
48 I: Integer; 
49 the begin 
50 for I: = 0 to do 500000 
51 is the begin 
52 is Form1.Canvas.Lock; 
56 is End; 
57 is
Form1.Canvas.Unlock 54 is; 
55 End; 
58 Procedure TForm1.btn1Click (Sender: TObject); 
59 the begin 
60 the MyThread: = TMyThread.Create (False); {instance of this class, is run immediately False, may be to True was added to enable MyThread.Resume} 
61 is End;
Copy the code

 

CriticalSection (critical section)

 uses SyncObjs; TCriticalSection class processing method.

Example: with three threads, in order to add ListBox 0 to 99.

Copy the code
 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls;
 8 
 9 type
10   TMyThread = class(TThread)
11   private
12     { Private declarations }
13   protected
14     procedure Execute; override; {执行}
15     procedure Run;  {运行}
16   end;
17   TForm1 = class(TForm)
18     btn1: TButton;
19     lst1: TListBox;
20     procedure btn1Click(Sender: TObject);
21     procedure FormDestroy(Sender: TObject);
22   private
23     { Private declarations }
Public 24 
54 is I: Integer;
Public Declarations} {25 
26 is End; 
27 
28 
29 
30 var 
31 is the Form1: TForm1; 
32 
33 is 
34 is Implementation 
35 
36. Dfm} {$ R & lt * 
37 [ 
38 is uses SyncObjs; 
39 
40 var 
41 is the MyThread: TMyThread; {statement thread} 
42 is the CS : TCriticalSection; {critical statement} 
43 is 
44 is 
45 Procedure TMyThread.Execute; 
46 is the begin 
47} {Place thread code here Wallpaper 
48 FreeOnTerminate: = True; {phrase plus thread runs out automatically annotated} 
49 the run; {operation} 
50 End ; 
51 is 
52 is Procedure TMyThread.Run; 
53 is var 
56 is CS.Enter; {I use, and the like other people}
The begin 55 
57 is for I: = 0 to 100 -. 1 do 
58 the begin 
59 Form1.lst1.Items.Add (the IntToStr (I)); 
60 End; 
61 is CS.Leave; {I run out, the next} 
62 is End; 
63 is 
Procedure TForm1.btn1Click 64 (Sender: TObject); 
65 the begin 
66 the CS: = TCriticalSection.Create; {critical instantiating} 
67 the MyThread: = TMyThread.Create (False); {instance of this class, run immediately to False, as True to enable dissecting MyThread.Resume} 
68 the MyThread: = TMyThread.Create (False); 
69 the MyThread: = TMyThread.Create (False); 
70 End; 
71 is 
72 
73 is TForm1.FormDestroy Procedure (Sender: TObject); 
74 the begin 
75 CS.Free; {release critical body} 
76 End; 
77 
78 End.
Copy the code

 

Mutex (mutual exclusion objects)

uses SyncObjs; with method of treating TMutex class (in the external circulation may be statements release determined execution order)

Example: three exclusive output a digital 0 to 2000 to form in a different position.

Copy the code
 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls;
 8 
 9 type
10   TMyThread = class(TThread)
11   private
12     { Private declarations }
13   protected
14     procedure Execute; override; {执行}
15     procedure Run;  {运行}
16   end;
17   TForm1 = class(TForm)
18     btn1: TButton;
19     procedure FormDestroy(Sender: TObject);
20     procedure btn1Click(Sender: TObject);
21   private
22     { Private declarations }
23   public
Public Declarations} {24 
25 End; 
26 is 
27 
28 
29 var 
30 the Form1: TForm1; 
31 is 
32 
33 is Implementation 
34 is 
35. Dfm} {$ R & lt * 
36 
37 [SyncObjs uses; 
38 is 
39 var 
40 the MyThread: TMyThread; {statement thread} 
41 is the Mutex : TMutex; {statement mutex} 
42 is F: Integer; 
43 is 
44 is 
45 Procedure TMyThread.Execute; 
46 is the begin 
47} {Place thread code here Wallpaper 
48 FreeOnTerminate: = True; {phrase plus thread runs out automatically annotated} 
49 run; {operation} 
50 End; 
51 is 
52 is Procedure TMyThread.Run; 
53 is var  
54 is I, Y: Integer;
55 the begin
Inc is an 56 is (F); 
57 is Y: * = 20 is F; 
58 for I: = 0 to 2000 do 
59 the begin 
60 IF Mutex.WaitFor (of INFINITE) = {wrSignaled the then determining function, and use any time} 
61 is the begin 
62 is the Form1. Canvas.Lock; 
63 is Form1.Canvas.TextOut (10, Y, the IntToStr (I)); 
64 Form1.Canvas.Unlock; 
65 Sleep (. 1); 
66 Mutex.Release; {release, who goes with} 
67 End ; 
68 End; 
69 End; 
70 
71 is TForm1.btn1Click Procedure (Sender: TObject); 
72 the begin 
73 is F: = 0; 
74 the Repaint; 
75 the Mutex: = TMutex.Create (False); {parameter is whether to allow the creator has mutex, typically} False 
78 the MyThread: = TMyThread.Create (False); 
76 the MyThread: = TMyThread.Create (False);
The MyThread 77: = TMyThread.Create (False);
End 79; 
80 
81 Procedure TForm1.FormDestroy (Sender: TObject); 
82 the begin 
83 Mutex.Free; {release mutex} 
84 End; 
85 
86 End.
Copy the code

 

Semaphore (semaphore signal or call)

 {DELPHI2007 not support semaphores, DELPHI2009 began support}

Copy the code
 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls;
 8 
 9 type
10   TForm1 = class(TForm)
11     Button1: TButton;
12     Edit1: TEdit;
13     procedure Button1Click(Sender: TObject);
14     procedure FormCreate(Sender: TObject);
15     procedure FormDestroy(Sender: TObject);
16     procedure Edit1KeyPress(Sender: TObject; var Key: Char);
17   end;
18 
19 var
20   Form1: TForm1;
21 
22 implementation
23 
24 {$R *.dfm}
25 
26 uses SyncObjs;
27 var
28   f: Integer;
29   MySemaphore: TSemaphore;
30 
31 function MyThreadFun(p: Pointer): DWORD; stdcall;
32 var
33   i,y: Integer;
34 begin
35   Inc(f);
36   y := 20 * f;
37   if MySemaphore.WaitFor(INFINITE) = wrSignaled then
38   begin
39     for i := 0 to 1000 do
40     begin
41       Form1.Canvas.Lock;
42       Form1.Canvas.TextOut(20, y, IntToStr(i));
43       Form1.Canvas.Unlock;
44       Sleep(1);
45     end;
46   end;
47   MySemaphore.Release;
48   Result := 0;
49 end;
50 
Procedure TForm1.Button1Click 51 is (Sender: TObject); 
52 is var 
53 is ThreadID: DWORD; 
54 is the begin 
55 IF the Assigned (MySemaphore) the then MySemaphore.Free; 
56 is MySemaphore: = TSemaphore.Create (nil, StrToInt (Edit1.Text),. 5, '); {created, the default parameters of a security thread running number to nil, can fill parameter 2, 3 is the total number of operating parameters, the parameters can be named a multi-process 4} 
57 is 
58 Self.Repaint; 
59 F: = 0 ; 
60 the CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID); 
61 is the CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID); 
62 is the CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID ); 
63 is the CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID); 
64 the CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID); 
65 End;  
66
67 {Edit allows only accept 12,345 five number}
68 procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
69 begin
70   if not CharInSet(Key, ['1'..'5']) then Key := #0;
71 end;
72 
73 procedure TForm1.FormCreate(Sender: TObject);
74 begin
75   Edit1.Text := '1';
76 end;
77 
78 procedure TForm1.FormDestroy(Sender: TObject);
79 begin
80   if Assigned(MySemaphore) then MySemaphore.Free;
81 end;
82 
83 end.
Copy the code

 

 

 Event (event object)

Note: Compared handling API, there is no way such a step after a pause start.

Copy the code
  1 unit Unit1;
  2 
  3 interface
  4 
  5 uses
  6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7   Dialogs, StdCtrls;
  8 
  9 type
 10   TMyThread = class(TThread)
 11   private
 12     { Private declarations }
 13   protected
 14     procedure Execute; override;
 15     procedure Run;
 16   end;
 17 
 18   TForm1 = class(TForm)
 19     btn1: TButton;
 20     btn2: TButton;
 21     btn3: TButton;
 22     btn4: TButton;
 23     procedure btn1Click(Sender: TObject);
 24     procedure FormDestroy(Sender: TObject);
 25     procedure btn2Click(Sender: TObject);
 26     procedure btn3Click(Sender: TObject);
 27     procedure btn4Click(Sender: TObject);
 28     procedure FormCreate(Sender: TObject);
 29   private
 30     { Private declarations }
 31   public
 32     { Public declarations }
 33   end;
 34 
 35 var
 36   Form1: TForm1;
 37 
 38 implementation
 39 
 40 {$R *.dfm}
 41 
 42 uses SyncObjs;
 43 
 44 var
 45   f:integer;
 46   MyEvent:TEvent;
 47   MyThread:TMyThread;
 48 
 TMyThread} {49 
 50 
 51 
 52 is Procedure TMyThread.Execute; 
 53 is the begin 
 54 is Inherited; 
 55 FreeOnTerminate: = True; {threads finished their cancellation} 
 56 is the Run; 
 57 is End; 
 58 
 59 Procedure TMyThread.Run; 
 60 var 
 61 is I, Y: Integer; 
 the begin 62 is 
 63 is Inc is an (F); 
 64 Y: 20 is = F *; 
 65 
 66 for I: = 0 to 20000 do 
 67 the begin 
 68 IF MyEvent.WaitFor (of INFINITE) = {wrSignaled the then determined with no event, with the event start and pause, play uniform event-related control thread} 
 69 the begin 
 70 Form1.Canvas.lock; 
 71 is Form1.Canvas.TextOut (10, Y, the IntToStr (I)); 
 72 Form1.Canvas.Unlock; 
 73 is Sleep (. 1); 
 74 end; 
 75 
 76 End; 
 77 
 78 End; 
 79 
 80 Procedure TForm1.btn1Click (Sender: TObject); 
 81 the begin 
 82 the Repaint; 
 83 F: = 0; 
 84 IF the Assigned (the MyEvent) the then MyEvent.Free; {if so, to destroy} 
 85 
 86 {Parameter a security setting, usually empty; 2 parameter is True pause can be manually controlled, once the control is immediately after the object Flase pause 
 87 3 parameters to run the object is established to True, after the control object creation is false suspend state , 4 parameter for the object name, for cross-process, when not in default ''} 
 88 MyEvent: = TEvent.Create (nil, True, True, ''); {create an event} 
 89 
 90 End; 
 91 
 92 Procedure TForm1.btn2Click (Sender: TObject); 
 93 var 
 94 ID: DWORD; 
 95 the begin 
 96 the MyThread: = TMyThread.Create (False); {Create a thread} 
 97 End; 
 98
 Procedure TForm1.btn3Click 99 (Sender: TObject); 
100 the begin
101 MyEvent.SetEvent; {} {Start Event Class PulseEvent not start after the first write light scanning Tan} 
102 End; 
103 
104 Procedure TForm1.btn4Click (Sender: TObject); 
105 the begin 
106 MyEvent.ResetEvent; {pause} 
107 End; 
108 
Procedure TForm1.FormCreate 109 (Sender: TObject); 
110 the begin 
111 btn1.Caption: = 'Create event'; 
112 btn2.Caption: = 'create a thread'; 
113 btn3.Caption: = 'start'; 
114 btn4.Caption: = 'pause'; 
115 End; 
1 16 
117 Procedure TForm1.FormDestroy (Sender: TObject); 
1 18 the begin 
119 MyEvent.Free; {release} 
120 End; 
121 
122 End.
Copy the code

to sum up:

Multithreading with TThread class and use the Uses syncobjs of TCriticalSection (critical area), TMutex (mutex), TSemaphore (signal objects, D2009 began there), many TEvent (event object) it is referenced API methods were some simplification, but there are some features missing, such as the event (event object) is missing a start after stepping pause function, but in the basic synchronization has been good enough, the other in the process of TThread Execute the class declaration, the plus FreeOnTerminate: = True; thread after the implementation of the sentence will automatically release, the method can also be set in the function code synchronize () was used to synchronize some non-thread-safe control objects, avoid multiple threads simultaneously on a target operation raises The problem.

Guess you like

Origin www.cnblogs.com/linjincheng/p/11609218.html