X
xiaodp
Unregistered / Unconfirmed
GUEST, unregistred user!
请熟手帮忙看看吧,我写的例子运行起来怎么还是单线程的效果?当我按不同的按钮应该能分别启动不同的线程才对的吧!谢谢了,虽然不早,我还会一直在线等候的。 ( 积分: 100 )<br />如题,代码如下:帮忙看看错在哪儿?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Db, ADODB,DosMove, ComCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyThread = class(TThread)
MyMemo : TMemo;
procedure show;virtual;abstract;
constructor Create(MyMemo1: TMemo);
end;
Thread1 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;
Thread2 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;
Thread3 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;
var
Form1: TForm1;
conString : String;
implementation
{$R *.dfm}
constructor TMyThread.create(MyMemo1:TMemo);
begin
inherited create(False);
MyMemo := MyMemo1;
FreeOnTerminate := true;
end;
procedure Thread1.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i));
end;
procedure Thread2.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i*2));
end;
procedure Thread3.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i*i));
end;
procedure Thread1.Execute;
begin
Synchronize(show);
end;
procedure Thread2.Execute;
begin
Synchronize(show);
end;
procedure Thread3.Execute;
begin
Synchronize(show);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
with Thread1.Create(Memo1)do
;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Memo2.Clear;
with Thread2.Create(Memo2)do
;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
Memo3.Clear;
with Thread3.Create(Memo3)do
;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Db, ADODB,DosMove, ComCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyThread = class(TThread)
MyMemo : TMemo;
procedure show;virtual;abstract;
constructor Create(MyMemo1: TMemo);
end;
Thread1 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;
Thread2 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;
Thread3 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;
var
Form1: TForm1;
conString : String;
implementation
{$R *.dfm}
constructor TMyThread.create(MyMemo1:TMemo);
begin
inherited create(False);
MyMemo := MyMemo1;
FreeOnTerminate := true;
end;
procedure Thread1.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i));
end;
procedure Thread2.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i*2));
end;
procedure Thread3.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i*i));
end;
procedure Thread1.Execute;
begin
Synchronize(show);
end;
procedure Thread2.Execute;
begin
Synchronize(show);
end;
procedure Thread3.Execute;
begin
Synchronize(show);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
with Thread1.Create(Memo1)do
;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Memo2.Clear;
with Thread2.Create(Memo2)do
;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
Memo3.Clear;
with Thread3.Create(Memo3)do
;
end;
end.