最简单的多线程。。。求教(50分)

  • 主题发起人 主题发起人 lynch2611
  • 开始时间 开始时间
L

lynch2611

Unregistered / Unconfirmed
GUEST, unregistred user!
功能:在一个Form显示的时候,在Form上的Edit中连续显示1--100。简单吧!!我是DELPHI初学者,还望DFW们多多指教。。。谢谢先
 
这个也要多线程来做?
 
指教。。。
 
to royal1442 :
请指教!!!
 
Timer的OnTime事件也是线程机制的:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Edit1.Tag := Edit1.Tag + 1;
Edit1.Text := IntToStr(Edit1.Tag);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
for i:=1 to 100do
begin
edit1.Text :=inttostr(i);
self.Repaint;
sleep(100);
end;
end;
 
你要是只想连续显示增长的数字
使用定时器足够了
如果是想测试多线程的话
可以从TThread类派生一个子类
然主线程在启动新线程之前将Form.Handle值给新线程
然后在新程中创建一个循环
循环内部等待一段时间后向Form.Handle邮寄一个自定义消息
在Form内创建一个消息响应函数以响应这个自定义的消息
然后在消息处理函数内部实际显示数据就可以了
 
大道至简
 
接受答案了.
 
楼主,动手做一个,蛮好玩的,等一下答你的题
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.
 
晕死,发完贴才了觉自己慢了这么多。
楼主说的可以实现。用线程就用线程嘛,反正是学习。
unit U_EditThread;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
Tf_EditThread = class(TForm)
Edit1: TEdit;
procedure FormShow(Sender: TObject);

end;

TEditThread=class(TThread)
private
FValue:Integer;
public
procedure Execute;override;
procedure UpdateEdit;
//用来在Edit1中显示
end;
var
f_EditThread: Tf_EditThread;
implementation
{$R *.dfm}
{ TEditThread }
procedure TEditThread.Execute;
var
i:Integer;
begin
FreeOnTerminate:=True;
for i:=0 to 100do
begin
FValue:=i;
Sleep(500);
//休息500毫秒,太快了会看不清
Synchronize(UpdateEdit);
//在这里显示
end;
end;

procedure TEditThread.UpdateEdit;
begin
f_EditThread.Edit1.Text:=IntToStr(FValue);
end;

procedure Tf_EditThread.FormShow(Sender: TObject);
begin
TEditThread.Create(false);
end;

end.
 
后退
顶部