constructor mythread.CreateIt(var l1:TLabel);
begin
  count:=1;
  Label1:=l1;
end;
你的线程没有建立,当然会出错!要在该构造函数中创建你的线程!

解决方案 »

  1.   

    { mythread }
    constructor mythread.CreateIt(var l1:TLabel);
    begin
      count:=1;
      Label1:=l1;
      inherited create(false);
    end;
    procedure mythread.show;
    begin
      form1.Label1.Caption:=inttostr(count);
      freeOnterminate:=true;
    end;
      

  2.   

    另外你的count要在form1中定义而不要在 form2中定义,每次button1.click 后inc(count)
      

  3.   

    问题1:你的count是做什么用的?
      

  4.   

    只是一个用来显示的数,没有任何意义
    我原来想显示该线程所占用的内存数,现在线程都运行不了了,只好改了
    我的mail:[email protected]
      

  5.   

    问题2:为什么要在show里调用 inherited...而它又包含在你的repeat里????问题3:你在show里用form1.Label1.Caption:=inttostr(count);那你线程本身的label又做什么用?问题4:你涉及到form1显示的,加了synchronize是得不到显示结果的
      

  6.   

    2:那是笔误,应该在CreateIt,我改了,可还是不行
    3:原来我想动态生成一个标签,然后传给线程显示,现在没有使用
    4:那怎么做呢
    我想得到结果
    5:给个qq咱们快点聊
      

  7.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      count:integer;implementation
    uses unit2;
    {$R *.DFM}procedure Tform1.creat(sender:tobject);
    begin
    count:=1;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      thread1:mythread;
    begin
      thread1.CreateIt(label1);
      inc(COUNT);
    end;end.............................................................
    unit Unit2;interfaceuses
      Classes,StdCtrls,SysUtils;type
      mythread = class(TThread)
      private
      //count:integer;
      Label1:TLabel;
        { Private declarations }
      protected
        procedure Execute; override;
        public
        constructor CreateIt(var l1:TLabel);
        procedure show;
      end;implementation
    uses unit1;
    { Important: Methods and properties of objects in VCL can only be used in a
      method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure mythread.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ mythread }
    constructor mythread.CreateIt(var l1:TLabel);
    begin
      //count:=1;
      Label1:=l1;
      inherited create(false);end;
    procedure mythread.show;
    begin
      form1.Label1.Caption:=inttostr(count);
      freeOnterminate:=true;
     // inherited create(true);
    end;
    procedure mythread.Execute;
    begin
    repeat
      synchronize(show);
      until Terminated;
      { Place thread code here }
    end;end.
      

  8.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      count:integer;implementation
    uses unit2;
    {$R *.DFM}procedure Tform1.creat(sender:tobject);
    begin
    count:=1;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      thread1:mythread;
    begin
      thread1.CreateIt(label1);
      inc(COUNT);
    end;end.............................................................
    unit Unit2;interfaceuses
      Classes,StdCtrls,SysUtils;type
      mythread = class(TThread)
      private
      //count:integer;
      Label1:TLabel;
        { Private declarations }
      protected
        procedure Execute; override;
        public
        constructor CreateIt(var l1:TLabel);
        procedure show;
      end;implementation
    uses unit1;
    { Important: Methods and properties of objects in VCL can only be used in a
      method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure mythread.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ mythread }
    constructor mythread.CreateIt(var l1:TLabel);
    begin
      //count:=1;
      Label1:=l1;
      inherited create(false);end;
    procedure mythread.show;
    begin
      form1.Label1.Caption:=inttostr(count);
      freeOnterminate:=true;
     // inherited create(true);
    end;
    procedure mythread.Execute;
    begin
    repeat
      synchronize(show);
      until Terminated;
      { Place thread code here }
    end;end.我运行了,没有报错!