~~菜鸟提问,有关多线程的~~

cloudlyanhart 2007-08-23 10:22:23
两个Memo,分别由两个线程控制,一个Memo循环输出1到10,每个数之间延迟1秒{sleep(1000)},另一个Memo循环输出11到20,每个数之间也延迟1秒,如何实现?以下是小弟写的,编译后点开始运行程序有误,麻烦高手看看
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2, Unit3;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Memo2: TMemo;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Memo1Thread: TMemo1Thread;
begin
Memo1Thread:= TMemo1Thread.Create(False);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
Memo2Thread: TMemo2Thread;
begin
Memo2Thread:= TMemo2Thread.Create(False);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
close;
end;

end.
------------------------------------------------------------
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TMemo1Thread = class(TThread)
Memo1: TMemo;
private
{ Private declarations }
protected
procedure Execute; override;
end;

implementation

{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,

Synchronize(UpdateCaption);

and UpdateCaption could look like,

procedure TMemo1Thread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }

{ TMemo1Thread }

procedure TMemo1Thread.Execute;
var
i:integer;
s:string;
begin
s:='';
for i:=1 to 10 do
begin
s:=inttostr(i);
memo1.Lines.Add(s);
sleep(1000);
end;
end;

end.
--------------------------------------------------------------
unit Unit3;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TMemo2Thread = class(TThread)
Memo2: TMemo;
private
{ Private declarations }
protected
procedure Execute; override;
end;

implementation

{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,

Synchronize(UpdateCaption);

and UpdateCaption could look like,

procedure TMemo2Thread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }

{ TMemo2Thread }

procedure TMemo2Thread.Execute;
var
m:integer;
n:string;
begin
n:='';
for m:=11 to 20 do
begin
n:=inttostr(m);
memo2.Lines.Add(n);
sleep(1000);
end;
end;

end.
...全文
122 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cloudlyanhart 2007-08-24
  • 打赏
  • 举报
回复
多谢楼上,但是我编译总是过不去,出在这条语句Memo1Thread:= TMemo1Thread.Create(Memo1);
系统提示[Error] Unit1.pas(44): This form of method call only allowed for class methods
不知何故?
cloudlyanhart 2007-08-24
  • 打赏
  • 举报
回复
昏,怎么揭不了贴?!
cloudlyanhart 2007-08-24
  • 打赏
  • 举报
回复
多谢楼上,揭帖了,两位收分吧
hmzgz81 2007-08-24
  • 打赏
  • 举报
回复
这样改就可以了!

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2, Unit3;

type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Memo1Thread: TMemo1Thread;
begin
Memo1Thread := TMemo1Thread.Create(False);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
Memo2Thread: TMemo2Thread;
begin
Memo2Thread := TMemo2Thread.Create(False);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
close;
end;

end.
end.
--------------------------------

unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TMemo1Thread = class(TThread) //删掉 线程里面的memo
private
{ Private declarations }
protected
procedure Execute; override;
end;

implementation

uses Unit1; //引用 Form1

{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,

Synchronize(UpdateCaption);

and UpdateCaption could look like,

procedure TMemo1Thread.UpdateCaption;
begin
Form1.Caption := "Updated in a thread ";
end; }

{ TMemo1Thread }

procedure TMemo1Thread.Execute;
var
i: integer;
s: string;
begin
s := '';
for i := 1 to 10 do
begin
s := inttostr(i);
Form1.memo1.Lines.Add(s);//直接往 Form1.memo1 里写
sleep(1000);
end;
end;

end.
------------------------------
unit Unit3;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TMemo2Thread = class(TThread) //删掉 线程里面的memo

private
{ Private declarations }
protected
procedure Execute; override;
end;

implementation

uses Unit1; //引用 Form1
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,

Synchronize(UpdateCaption);

and UpdateCaption could look like,

procedure TMemo2Thread.UpdateCaption;
begin
Form1.Caption := "Updated in a thread ";
end; }

{ TMemo2Thread }

procedure TMemo2Thread.Execute;
var
m: integer;
n: string;
begin
n := '';
for m := 11 to 20 do
begin
n := inttostr(m);
Form1.memo2.Lines.Add(n); //直接往 Form1.memo2 里写
sleep(1000);
end;
end;

end.
cloudlyanhart 2007-08-24
  • 打赏
  • 举报
回复
上面的错误没有了,新的错误又来了。。哎。。算了。。今天5点起前揭帖
cloudlyanhart 2007-08-23
  • 打赏
  • 举报
回复
没人理我。。是这问题太傻了么。。
cloudlyanhart 2007-08-23
  • 打赏
  • 举报
回复
一楼自己的,我上面那些基本是胡写的,刚开始看这块,希望有人能就上面的要求写个正确的程序给小弟看看,无限感激
lvloj 2007-08-23
  • 打赏
  • 举报
回复
//需要改的地方都列出来了, 自己看看, 多动手做下就清楚了.

type
TMemo1Thread = class(TThread)
private
{ Private declarations }
FStr: string;
Memo1: TMemo;
procedure AddToMemo;
protected
procedure Execute; override;
public
constructor Create(const Memo: TMemo);
end;

........

procedure TMemo1Thread.AddToMemo;
begin
memo1.Lines.Add(FStr);
end;

procedure TMemo1Thread.Create(const Memo: TMemo);
begin
inherited Create(True);
Memo1 := Memo;

Resume;
end;
procedure TMemo1Thread.Execute;
var
i:integer;
begin
for i:=1 to 10 do
begin
FStr:=inttostr(i);
Synchronize(AddToMemo);//
sleep(1000);
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
Memo1Thread: TMemo1Thread;
begin
Memo1Thread:= TMemo1Thread.Create(Memo1);

end;

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧