谁能给我一个多线程的完整例子!200!!!!(200分)

  • 主题发起人 主题发起人 chw2100
  • 开始时间 开始时间
C

chw2100

Unregistered / Unconfirmed
GUEST, unregistred user!
我的EMAIL地址是chw2100@hotmail.com! 谢谢!
 
Delphi带的例子里面就有了。
 
这是一个多线程画图的例子,或者你看看DELPHI的DEMO目录,里面有多线程排序的例子。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus;
type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
sadf1: TMenuItem;
N11: TMenuItem;
N21: TMenuItem;
N31: TMenuItem;
N41: TMenuItem;
ColorDialog1: TColorDialog;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure N11Click(Sender: TObject);
procedure N21Click(Sender: TObject);
procedure N31Click(Sender: TObject);
procedure N41Click(Sender: TObject);
private
{ Private declarations }
ThreadList :TList;
public
{ Public declarations }
end;

TDrawThread=Class(TThread)
private
FColor :TColor;
FForm :TForm;
public
Constructor Create(AForm :TForm;
AColor :TColor);
procedure Execute;override;
end;

var
MainForm: TMainForm;
implementation
{$R *.DFM}
Constructor TDrawThread.Create(AForm :TForm;
AColor :TColor);
begin
FColor :=AColor;
FForm :=AForm;
Inherited Create(False);
end;

procedure TDrawThread.Execute;
var
p1,p2 :TPoint;
procedure GetRandomColor;
var
MaxX,MaxY :Integer;
begin
MaxX :=FForm.ClientWidth;
MaxY :=FForm.ClientHeight;
p1.x :=Random(MaxX);
p1.y :=Random(MaxY);
p2.x :=Random(MaxX);
p2.y :=Random(MaxY);
end;

begin
FreeOnTerminate :=True;
While not (Terminated or Application.Terminated)do
begin
GetRandomColor;
with FForm.Canvasdo
begin
Lock;
Pen.Color :=FColor;
MoveTo(p1.x,p1.y);
LineTo(p2.x,p2.y);
unLock;
end;
end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
ThreadList :=TList.Create;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
N41Click(nil);
ThreadList.Free;
end;

procedure TMainForm.N11Click(Sender: TObject);
begin
if ColorDialog1.Execute then
ThreadList.Add(TDrawThread.Create(Self,ColorDialog1.Color));
end;

procedure TMainForm.N21Click(Sender: TObject);
begin
TDrawThread(ThreadList[ThreadList.Count-1]).Terminate;
ThreadList.Delete(ThreadList.Count-1);
end;

procedure TMainForm.N31Click(Sender: TObject);
var
I :Integer;
begin
for I :=1 to 30do
ThreadList.Add(TDrawThread.Create(Self,Random(MaxInt)));
end;

procedure TMainForm.N41Click(Sender: TObject);
var
I :Integer;
begin
Cursor :=crHourGlass;
try
for I :=ThreadList.Count-1do
wnto 0do
begin
TDrawThread(ThreadList).Terminate;
TDrawThread(ThreadList).WaitFor;
end;
ThreadList.Clear;
finally
Cursor :=crDefault;
end;
end;

initialization
Randomize;
end.
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=843625
project代码已发。
 
我已给你发了一个了,有什么收获,通知一声阿,^_^
 
多人接受答案了。
 
后退
顶部