delphi中写多线程:在两个线程中都输出1-20到memo中,如何显示除下列效果
2个回答
展开全部
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
btnStart: TButton;
Memo1: TMemo;
procedure btnStartClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TTestThread=class(TThread)
private
Answer:Integer;
protected
procedure GiveAnswer;
procedure Execute;override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnStartClick(Sender: TObject);
var
t1,t2:TTestThread;
begin
t1:=TTestThread.Create;
t2:=TTEstThread.Create;
end;
{ TTestThread }
procedure TTestThread.Execute;
var
i:Integer;
begin
inherited;
FreeOnTerminate:=True;
Answer:=0;
for i:= 1 to 20 do
begin
Inc(Answer);
if Terminated then break;
Synchronize(GiveAnswer);
end;
end;
procedure TTestThread.GiveAnswer;
begin
Form1.Memo1.Lines.Add(IntToStr(Answer));
end;
end.
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
btnStart: TButton;
Memo1: TMemo;
procedure btnStartClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TTestThread=class(TThread)
private
Answer:Integer;
protected
procedure GiveAnswer;
procedure Execute;override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnStartClick(Sender: TObject);
var
t1,t2:TTestThread;
begin
t1:=TTestThread.Create;
t2:=TTEstThread.Create;
end;
{ TTestThread }
procedure TTestThread.Execute;
var
i:Integer;
begin
inherited;
FreeOnTerminate:=True;
Answer:=0;
for i:= 1 to 20 do
begin
Inc(Answer);
if Terminated then break;
Synchronize(GiveAnswer);
end;
end;
procedure TTestThread.GiveAnswer;
begin
Form1.Memo1.Lines.Add(IntToStr(Answer));
end;
end.
追问
你这个是将两个线程的工作分开了、、我的意思是两个线程中都循环1-20并且都向memo中输入结果,怎么才会显示出上图效果?
追答
以上的代码只是展示出多线程的效果。
如果需要一个线程输出完毕,再由另一个线程输出,有多种办法。这里列出一种:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
btnStart: TButton;
Memo1: TMemo;
procedure btnStartClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TTestThread=class(TThread)
private
Answer:Integer;
AnswerS:String;
protected
procedure GiveAnswer;
procedure OutAnswers;
procedure Execute;override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnStartClick(Sender: TObject);
var
t1,t2:TTestThread;
begin
t1:=TTestThread.Create;
t2:=TTEstThread.Create;
end;
{ TTestThread }
procedure TTestThread.Execute;
var
i:Integer;
begin
inherited;
FreeOnTerminate:=True;
Answer:=0;
AnswerS:='';
for i:= 1 to 20 do
begin
Inc(Answer);
if Terminated then break;
AnswerS:=AnswerS+#13#10+IntToStr(Answer);
end;
Synchronize(OutAnswers);
end;
procedure TTestThread.GiveAnswer;
begin
Form1.Memo1.Lines.Add(IntToStr(Answer));
end;
procedure TTestThread.OutAnswers;
begin
Form1.Memo1.Lines.Add(AnswerS);
end;
end.
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
“如何显示除下列效果”
什么意思?
什么意思?
追问
如何显示出下列效果
追答
显示那图的效果?
每个线程都循环一次,那么就应该是1-20的每个数在Memo里面都有2个吧?而不应该是一个吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询