求助:DELPHI多线程的问题

学习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. 

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐