线程单元代码:
type
sortthread = class(TThread)
private
fmemo:Tmemo;
procedure add();
protected
procedure Execute; override;
end;implementation
uses unit1;
procedure sortthread.add;
var
i:integer;
begin
for i:=0 to 40000 do
begin
form1.memo1.Lines.Clear;
form1.Memo1.lines.add(inttostr(i));
end;
end;procedure sortthread.Execute;
begin
freeonterminate:=true;
while not terminated do
begin
synchronize(add);
end;
end;主程序调用单元:
procedure TForm1.Button1Click(Sender: TObject);
var
newthread:sortthread;
begin
newthread:=sortthread.create(false);
end;
type
sortthread = class(TThread)
private
fmemo:Tmemo;
procedure add();
protected
procedure Execute; override;
end;implementation
uses unit1;
procedure sortthread.add;
var
i:integer;
begin
for i:=0 to 40000 do
begin
form1.memo1.Lines.Clear;
form1.Memo1.lines.add(inttostr(i));
end;
end;procedure sortthread.Execute;
begin
freeonterminate:=true;
while not terminated do
begin
synchronize(add);
end;
end;主程序调用单元:
procedure TForm1.Button1Click(Sender: TObject);
var
newthread:sortthread;
begin
newthread:=sortthread.create(false);
end;
begin
synchronize(add);
end;
这里有问题,线程不结束就执行Add方法。
线程一直在等Add结束,可同时有一直在执行Add。所以成了死循环了
如果只是执行一次把while却掉,就
synchronize(add);