2

从一个 tthread 类对象更新主窗体中的状态栏的最佳方法是什么。例如,我有一个 TThread 对象,它制作了大量的东西,我希望根据你所做的软的详细消息出现在状态栏上。

4

1 回答 1

9

使用对回调方法的引用创建线程,如果已分配,则可以称为同步。

例子;

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TInfoMessageCall=Procedure (const info:String) of object;

  TMyThread=Class(TThread)
    Constructor Create(Susp:Boolean;CallBack:TInfoMessageCall);overload;
    Procedure Execute;override;
    private
    FMessage:String;
    FInfoProc:TInfoMessageCall;
    Procedure CallCallBack;

  End;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure ACallBack(const s: String);
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TMyThread }

Procedure TForm1.ACallBack(Const s:String);
begin
  Caption := s;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  With TMyThread.Create(false,ACallBack) do
    FreeOnTerminate := true;
end;


procedure TMyThread.CallCallBack;
begin
  if Assigned(FInfoProc) then  FInfoProc(FMessage);
end;

constructor TMyThread.Create(Susp: Boolean; CallBack: TInfoMessageCall);
begin
   Inherited Create(Susp);
   FInfoProc := CallBack;
end;

procedure TMyThread.Execute;
var
 i:Integer;
begin
  inherited;
    for I := 1 to 10 do
          begin
            FMessage := Format('Info %d',[i]);
            Synchronize(CallCallBack);
            Sleep(200);
          end;
end;


end.
于 2013-06-14T04:58:29.093 回答