程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 多線程編程(18) - 再從一個小例子出發

多線程編程(18) - 再從一個小例子出發

編輯:Delphi

前面的例子都是讓若干線程做同樣的事情, 下面這個例子中的三個線程將分別在三個畫板上隨機畫不同顏色的橢圓.

接下來的很多事情我想要基於這個例子來做.

本例效果圖:

代碼文件:

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
PaintBox2: TPaintBox;
PaintBox3: TPaintBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
h1,h2,h3: THandle;
{第一個線程的入口函數: 畫紅色橢圓}
function ThreadFun1(p: Pointer): Integer; stdcall;
var
i, x1,y1,x2,y2: Integer;
begin
Form1.PaintBox1.Canvas.Brush.Color := clRed;
for i := 0 to 50000 do with Form1.PaintBox1 do
begin
x1 := Random(Width); y1 := Random(Height);
x2 := Random(Width); y2 := Random(Height);
Canvas.Lock;
Canvas.Ellipse(x1,y1,x2,y2);
Canvas.Unlock;
Sleep(0);
end;
Result := 0;
end;
{第二個線程的入口函數: 畫綠色橢圓}
function ThreadFun2(p: Pointer): Integer; stdcall;
var
i, x1,y1,x2,y2: Integer;
begin
Form1.PaintBox2.Canvas.Brush.Color := clGreen;
for i := 0 to 50000 do with Form1.PaintBox2 do
begin
x1 := Random(Width); y1 := Random(Height);
x2 := Random(Width); y2 := Random(Height);
Canvas.Lock;
Canvas.Ellipse(x1,y1,x2,y2);
Canvas.Unlock;
Sleep(0);
end;
Result := 0;
end;
{第三個線程的入口函數: 畫藍色橢圓}
function ThreadFun3(p: Pointer): Integer; stdcall;
var
i, x1,y1,x2,y2: Integer;
begin
Form1.PaintBox3.Canvas.Brush.Color := clBlue;
for i := 0 to 50000 do with Form1.PaintBox3 do
begin
x1 := Random(Width); y1 := Random(Height);
x2 := Random(Width); y2 := Random(Height);
Canvas.Lock;
Canvas.Ellipse(x1,y1,x2,y2);
Canvas.Unlock;
Sleep(0);
end;
Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ID: DWORD;
begin
h1 := CreateThread(nil, 0, @ThreadFun1, nil, 0, ID);
h2 := CreateThread(nil, 0, @ThreadFun2, nil, 0, ID);
h3 := CreateThread(nil, 0, @ThreadFun3, nil, 0, ID);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
CloseHandle(h1);
CloseHandle(h2);
CloseHandle(h3);
end;
end.

窗體文件:

object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 206
ClientWidth = 371
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object PaintBox1: TPaintBox
Left = 8
Top = 8
Width = 114
Height = 153
end
object PaintBox2: TPaintBox
Left = 128
Top = 8
Width = 114
Height = 153
end
object PaintBox3: TPaintBox
Left = 248
Top = 8
Width = 114
Height = 153
end
object Button1: TButton
Left = 288
Top = 172
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end

可以借助入口函數的參數, 把這個程序簡化一下(窗體和運行效果不變):

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
PaintBox2: TPaintBox;
PaintBox3: TPaintBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
h1,h2,h3: THandle;
{線程的入口函數: 不同的線程畫不同顏色的橢圓}
function ThreadFun(p: Pointer): Integer; stdcall;
var
i,x1,y1,x2,y2: Integer;
paint: TPaintBox;
begin
case Integer(p) of
1: begin
paint := Form1.PaintBox1;
paint.Canvas.Brush.Color := clRed;
end;
2: begin
paint := Form1.PaintBox2;
paint.Canvas.Brush.Color := clGreen
end;
3: begin
paint := Form1.PaintBox3;
paint.Canvas.Brush.Color := clBlue;
end;
end;
for i := 0 to 5000 do with paint do
begin
x1 := Random(Width); y1 := Random(Height);
x2 := Random(Width); y2 := Random(Height);
Canvas.Lock;
Canvas.Ellipse(x1,y1,x2,y2);
Canvas.Unlock;
Sleep(0);
end;
Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ID: DWORD;
begin
h1 := CreateThread(nil, 0, @ThreadFun, Ptr(1), 0, ID);
h2 := CreateThread(nil, 0, @ThreadFun, Ptr(2), 0, ID);
h3 := CreateThread(nil, 0, @ThreadFun, Ptr(3), 0, ID);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
CloseHandle(h1);
CloseHandle(h2);
CloseHandle(h3);
end;
end.

多用點數組, 再簡化一下(窗體與效果一樣):

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
PaintBox2: TPaintBox;
PaintBox3: TPaintBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
colors: array[0..2] of TColor = (clRed, clGreen, clBlue);
var
hArr: array[0..2] of THandle;
panitArr: array[0..2] of TPaintBox;
function ThreadFun(p: Pointer): Integer; stdcall;
var
i,n,x1,y1,x2,y2: Integer;
begin
n := Integer(p);
panitArr[n].Color := colors[n];
for i := 0 to 5000 do with panitArr[n] do
begin
x1 := Random(Width); y1 := Random(Height);
x2 := Random(Width); y2 := Random(Height);
Canvas.Lock;
Canvas.Ellipse(x1,y1,x2,y2);
Canvas.Unlock;
Sleep(0);
end;
Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ID: DWORD;
i: Integer;
begin
panitArr[0] := PaintBox1;
panitArr[1] := PaintBox2;
panitArr[2] := PaintBox3;
for i := 0 to Length(hArr) - 1 do
hArr[i] := CreateThread(nil, 0, @ThreadFun, Ptr(i), 0, ID);
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
begin
for i := 0 to Length(hArr) - 1 do CloseHandle(hArr[i]);
end;
end.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved