DELPHI 多線程(TThread類的實現)實例


再做個實例總結下:

用多線程類實現,在三個PaintBox上畫橢圓。

  1 unit Unit1;
  2 
  3 interface
  4 
  5 uses
  6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7   Dialogs, StdCtrls, ExtCtrls;
  8 
  9 type
 10   TMyThread = class(TThread)
 11     private
 12     FNum:Integer;
 13     protected
 14       procedure Execute;override;
 15       procedure Run;
 16       procedure SetNum(Value:integer);  {設置Num}
 17       function GetNum:integer;          {獲取Num}
 18       constructor Create(Aswith:Boolean;ANum:Integer);  {給創建方法添加一個參數用於傳遞}
 19     public
 20   end;
 21 
 22   TForm1 = class(TForm)
 23     pb1: TPaintBox;
 24     pb2: TPaintBox;
 25     pb3: TPaintBox;
 26     btn1: TButton;
 27     procedure btn1Click(Sender: TObject);
 28     procedure FormDestroy(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 const
 45    colors:array[0..2] of TColor =(clRed,clGreen,clBlue);
 46 
 47 var
 48   MyThread:TMyThread;
 49   paintArr:array[0..2] of TPaintBox;
 50 //  MyThreadArr:array[0..2] of TMyThread;  {也可用數組多個線程進行}
 51   Mutex:TMutex;
 52 
 53 
 54 procedure TForm1.btn1Click(Sender: TObject);
 55 var
 56   i:integer;
 57 begin
 58   if Assigned(Mutex) then Mutex.Free;   {如果存在,先釋放}
 59   Mutex:=TMutex.Create(False);           {創建互斥體,參數為創建者先不占用}
 60   Repaint;                               {重畫窗體}
 61   paintArr[0]:=pb1;                       {把PaintBox1給數組paintArr[0]}
 62   paintArr[1]:=pb2;
 63   paintArr[2]:=pb3;
 64   for i := Low(paintArr )to High(paintArr) do
 65   begin
 66     MyThread:=TMyThread.Create(False,i);  {如果聲明了線程數組類,可以在這里一起做循環多個線程操作}
 67   end;
 68 end;
 69 
 70 { TMyThread }
 71 
 72 
 73 
 74 constructor TMyThread.Create(ASwith: Boolean; ANum: Integer);
 75 begin
 76   inherited Create(Aswith);   {繼承並傳參}
 77   SetNum(ANum);               {設置字段Num}
 78 end;
 79 
 80 procedure TMyThread.Execute;
 81 begin
 82   inherited;
 83   FreeOnTerminate:=True;
 84   Run;
 85 end;
 86 
 87 function TMyThread.GetNum: integer;
 88 begin
 89   Result:=FNum;
 90 end;
 91 
 92 procedure TMyThread.Run;
 93 var
 94   i,n,x1,x2,y1,y2:integer;
 95 begin
 96   n:=GetNum;
 97   paintArr[n].Color:=Colors[n];
 98   for i := 0 to 200 do
 99   begin
100     if Mutex.WaitFor(INFINITE)=wrSignaled then  {判斷互斥體有沒有被占用}
101     begin
102       with paintArr[n] do
103       begin
104         x1:=Random(Width);y1:=Random(Height);
105         x2:=Random(Width);y2:=Random(Height);
106         Canvas.Lock;
107         Canvas.Ellipse(x1,y1,x2,y2);
108         Canvas.Unlock;
109         Sleep(10);
110       end;
111     end;
112     Mutex.Release;  {釋放放這里為每進行一次,釋放,誰拿到誰用}
113   end;
114 //   Mutex.Release;  {釋放放這里為每進行一輪循環,釋放,相當於排隊進行}
115 end;
116 
117 procedure TMyThread.SetNum(Value: integer);
118 begin
119   FNum:=Value;
120 end;
121 
122 procedure TForm1.FormDestroy(Sender: TObject);
123 begin
124   Mutex.Free;
125 end;
126 
127 end.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM