楼主,动手做一个,蛮好玩的,等一下答你的题
unit U_MultiThread;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls, StdCtrls;
type
Tf_MultiThread = class(TForm)
pnl: TPanel;
//Align:=alTop;
pgc: TPageControl;
//Align:=alClient;
btnNew: TButton;
//这几个Button都放在pnl上
btnClose: TButton;
btnPauseThread: TButton;
btnThreadContinue: TButton;
procedure btnCloseClick(Sender: TObject);
procedure btnNewClick(Sender: TObject);
procedure btnPauseThreadClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnThreadContinueClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyTabSheet=class(TTabSheet) //自定义一个TabSheet,好显示MyThread的运行过程
private
FMemo:TMemo;
//MyThread的运行过程在此显示
public
property Memo:TMemo read FMemo write FMemo;
constructor create(AOwner:TComponent;AParent:TPageControl);
destructor destroy;override;
end;
TMyThread=class(TThread)
private
FMemo:TMemo;
//此变量用来指向MyTabSheet中的Memo
FValue:Integer;
public
constructor create(MyTabSheet:TMyTabSheet);
procedure execute;override;
procedure updateMemo;
end;
var
f_MultiThread: Tf_MultiThread;
arrThread:array of TMyThread;
//声明一个线程数组
iThread:integer;
implementation
{$R *.dfm}
procedure Tf_MultiThread.btnCloseClick(Sender: TObject);
begin
//这里有危险。当前MyTabSheet被Free后,MyTabSheet的Memo也被Free了,
//但当前的MyThread还要同步显示,MyThread的私有变量FMemo指向了一个未知的地址
// pgc.ActivePage.Free;
end;
procedure Tf_MultiThread.btnNewClick(Sender: TObject);
var
myTabSheet:TMyTabSheet;
begin
if iThread>=10 then
exit;
//最多允许10个线程
//为线程数组赋值
arrThread[iThread]:=TMyThread.Create(TMyTabSheet.Create(self,pgc));
iThread:=iThread+1;
end;
{ TMyTabSheet }
constructor TMyTabSheet.create(AOwner: TComponent;AParent:TPageControl);
begin
inherited create(AOwner);
PageControl:=AParent;
Parent:=AParent;
Caption:=IntToStr(PageIndex);
AParent.ActivePage:=self;
FMemo:=TMemo.Create(self);
with FMemodo
begin
Align:=alClient;
Text:='';
Parent:=self;
//这里的Self是则创建的MyTabSheet
end;
end;
destructor TMyTabSheet.destroy;
begin
FreeAndNil(FMemo);
inherited;
end;
{ TMyThread }
constructor TMyThread.create(MyTabSheet: TMyTabSheet);
begin
FMemo:=MyTabSheet.Memo;
inherited create(false);
end;
procedure TMyThread.execute;
var
i:integer;
begin
FreeOnTerminate:=True;
for i:=0 to 30000do
begin
FValue:=i;
sleep(random(20));
Synchronize(updatememo);
//在MyTabSheet的Memo中同步显示
end;
inherited;
end;
procedure TMyThread.updateMemo;
begin
FMemo.Lines.Add(intToStr(FValue));
end;
procedure Tf_MultiThread.FormCreate(Sender: TObject);
begin
SetLength(arrThread,10);
//线程数组初始化
end;
procedure Tf_MultiThread.btnPauseThreadClick(Sender: TObject);
begin
arrThread[pgc.ActivePageIndex].Suspend;
//暂停当前线程
end;
procedure Tf_MultiThread.btnThreadContinueClick(Sender: TObject);
begin
arrThread[pgc.ActivePageIndex].Resume;
//继续运行当前线程
end;
procedure Tf_MultiThread.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
var
i:Integer;
begin
for i:=0 to pgc.ComponentCount-1do
begin
if Components is TMyTabSheet then
(pgc.ActivePage as TMyTabSheet).Free;
end;
end;
end.