delphi有个例子,写的非常好,你可以看看,这个问题一下说不清楚

解决方案 »

  1.   

    guoyuzhang
    我知道那个程序,可是太麻烦;其实我要实现的功能很简单,我在调用时老出错,我声明的TThreed是这样的:
    unit MyThread;interfaceuses
      Classes;type
      MyThread = class(TThread)
      public
        constructor create;
      private
        { Private declarations }
      protected
        procedure Execute; override;
        procedure dobuildtree;
      public
        constructor create;
      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.create;
    begin
        inherited create(False);
        FreeOnTerminate:=True;
    end;procedure MyThread.dobuildtree;
    begin
            with form1 do
       begin
        table1.open;
       table1.filter:='';
       Gauge.MaxValue:=table1.recordcount;
       Gauge.Progress:=1;
       RootNode:=fcTreeview1.Items.AddChild(nil,'B0000000-国家邮政局');
       buildTree(RootNode);
       end;
    end;procedure MyThread.Execute;
    begin
     inherited create(False);
       dobuildtree;
      { Place thread code here }
    end;end.
    你看是什么毛病,谢谢!
      

  2.   

    首先,VCL库不支持多线程,所以对VCL控件的操作都必须调用
    Synchronize(dobuildtree);其次,TThread的Synchronize只是给主窗口发送一条消息,然后等待主线程来执行,其原理只是通过主线程的消息循环来达到同步的目的。因此,你的做法没有任何意义(事实上没有用到多线程)。最后,你的dobuildtree函数依然应该放在主线程中执行,如果该函数调用时间很长的话,担心画面无法刷新的话,只要多调用几次Application.ProcessMessages()就可以了。