学习TTHREAD的用法,照着写,但是结果好像只输出了最后一个线程
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure AddLogInfo(rybh: string);
public
{ Public declarations }
end;
TMyThread = class(TThread)
private
str: string; //日志信息
procedure AddLog; //添加日志
public
procedure Execute; override;
end;
var
Form1: TForm1;
sl:Integer;
Strlog:string;
implementation
{$R *.dfm}
procedure TForm1.AddLogInfo(rybh: string);
begin
Form1.ListBox1.Items.Add('返回值:'+rybh);
end;
procedure TMyThread.Execute;
var
i: Integer;
begin
FreeOnTerminate := True; {这可以让线程执行完毕后随即释放}
i:=sl;
Str:='D'+inttostr(I);
StrLog:=Str;
synchronize(AddLog); //发送成功给捕捉
end;
procedure TMyThread.AddLog;
begin
Form1.AddLogInfo(StrLog);
end;
procedure TForm1.Button1Click(Sender: TObject);
var ib: Integer;
begin
ib:=0;
while ib<10 do
begin
sl:=ib;
TMyThread.Create(False);
sleep(50);
ib:=ib+1;
end;
end;
end.