1. build a thread Unit, just like this:unit GameThread;interfaceuses Classes,stdctrls;type TGameThread = class(TThread) private { Private declarations } protected procedure Execute; override; public Destructor Destroy; override; Constructor Create();override; end;implementation uses Main,SysUtils,Dialogs,Windows;{ TGameThread } Constructor TGameThread.Create(); begin inherited Create(False); dosomething.. end;destructor TGameThread.Destroy; begin inherited Destroy; Dosomething; end;procedure TGameThread.Execute; begin DoSomeThing ...... end; end; 2.in main Unit,Call it,just like this:procedure TForm1.btnSearchClick(Sender: TObject); var GameThread:TGameThread; begin GameThread:=TGameThread.Create(); end;
参考一下《Delphi 5开发人员指南》,上面介绍的非常详细!!!
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } threadlist:Tlist; public { Public declarations } end; TtestThread= class(Tthread) private i:integer; public procedure loadpicure; constructor create(index:integer); procedure clearpicure; procedure execute;override; end; var Form1: TForm1;implementation{$R *.dfm}{ TtestThread }constructor TtestThread.create(index: integer); begin i:=index; Inherited create(false); end;procedure TtestThread.execute; begin FreeOnTerminate:=true; while true do begin Synchronize(somefunc); sleep(100); end; end;procedure TtestThread.somefunc; begin case i of
…… //略去
end;procedure TForm1.Button1Click(Sender: TObject); var i:integer; begin try for i:=1 to 6 do threadlist.Add(TtestThread.Create(i)); except showmessage('erro'); end end;procedure TForm1.FormCreate(Sender: TObject); begin threadlist:=TList.Create; end;end.
Classes,stdctrls;type
TGameThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
Destructor Destroy; override;
Constructor Create();override;
end;implementation
uses Main,SysUtils,Dialogs,Windows;{ TGameThread }
Constructor TGameThread.Create();
begin
inherited Create(False);
dosomething..
end;destructor TGameThread.Destroy;
begin
inherited Destroy;
Dosomething;
end;procedure TGameThread.Execute;
begin
DoSomeThing ......
end;
end;
2.in main Unit,Call it,just like this:procedure TForm1.btnSearchClick(Sender: TObject);
var
GameThread:TGameThread;
begin
GameThread:=TGameThread.Create();
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
threadlist:Tlist;
public
{ Public declarations }
end;
TtestThread= class(Tthread)
private
i:integer;
public
procedure loadpicure;
constructor create(index:integer);
procedure clearpicure;
procedure execute;override;
end;
var
Form1: TForm1;implementation{$R *.dfm}{ TtestThread }constructor TtestThread.create(index: integer);
begin
i:=index;
Inherited create(false);
end;procedure TtestThread.execute;
begin
FreeOnTerminate:=true;
while true do
begin
Synchronize(somefunc);
sleep(100);
end;
end;procedure TtestThread.somefunc;
begin
case i of
……
//略去
end;procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
try
for i:=1 to 6 do
threadlist.Add(TtestThread.Create(i));
except
showmessage('erro');
end
end;procedure TForm1.FormCreate(Sender: TObject);
begin
threadlist:=TList.Create;
end;end.