如何在一个窗体中动态创建一个线程,这个线程的工作就是不停的更新该窗体的某个label或者其他控件的的内容

解决方案 »

  1.   

    unit Unit2;interfaceuses
      Classes,StdCtrls,SysUtils;type
      test = class(TThread)
      private
        { Private declarations }
        FLabel: TLabel;
      public
        Constructor Create(aLabel: TLabel);
      protected
        procedure Execute; override;
      end;implementation{ test }constructor test.Create(aLabel: TLabel);
    begin
      FLabel:=aLabel;
      inherited Create(false);
    end;procedure test.Execute;
    begin
      while not Terminated do
      begin
        self.FLabel.Caption:=DateTimeToStr(Now);
        sleep(1000);
      end;
      { Place thread code here }
    end;end.
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,shellapi, StdCtrls,Unit2;type
      TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
        t:test;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    {$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      t:= test.Create(self.Label1);
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    t.Terminate;
    end;end.
      

  2.   


    unit Unit2;interfaceuses
      Classes,StdCtrls,SysUtils;type
      test = class(TThread)
      private
        { Private declarations }
      public
        procedure Fresh;
      protected
        procedure Execute; override;
      end;implementationuses Unit1;{ test }procedure test.Fresh;
    begin
      Form1.Label1.Caption := DateTimeToStr(Now);
    end;procedure test.Execute;
    begin
      while not Terminated do
      begin
        WaitForSingleObject(Event, 1000);
        Synchronize(Fresh);
      end;
    end;end.unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,shellapi, StdCtrls, Unit2;type
      TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        Button2: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
        t:test;
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      Event: THandle;implementation
    {$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      Event := CreateEvent(nil, false, false, nil);
      SetEvent(Event);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      t:= test.Create(false);
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      if not t.Terminated then
      begin
        SetEvent(Event);
        t.Terminate;
        t.WaitFor;
        t.Free;
      end;
    end;
      

  3.   

    就是在执行线程显示后,再执行select 语句(只是提取的内容很多),
      

  4.   

    To:玉米老兄!您写得比较规范,但我有一个问题请教您:在设计期已经确定用单线程来访问的控件还用加Synchronize吗?我认为如果在UI线程中该控件仅仅是显示,而不参与修改的话,是不是直接将其的引用传到线程中更好一些!