Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - Updating status bar from thread

What is the best way to update a status bar in mainform from one tthread class object. For Example, I have one TThread object that make a very big quanty of stuff and i want that the verbose message acording to what soft do you do appear on status bar.

like image 654
David A Avatar asked Dec 07 '25 23:12

David A


1 Answers

Create the thread with a reference to a callback method, which can be called synchonized if assigned.

Example;

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.
like image 125
bummi Avatar answered Dec 10 '25 16:12

bummi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!