Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VCL application main thread?

Status
Not open for further replies.

Ricky Nelson

Programmer
Jul 31, 2018
1
AU
I am not familiar with thread programming...for example I have a VCL Form application, created a TThread on main form.
Which one is the main thread? The Main form?
When I call Synchronize(...) the function inside the bracket has to be the procedure from Main form?

Sorry if this is trivial.
 
Any of your VCL runs in the main thread for the application. You want to keep anything lengthy away from that main thread so the forms can handle messages, be drawn upon, and so on. Hence, threads. TThread is just one way of doing it. No apologies, as nothing to do with threads is really trivial. Anyhow, Synchronize is of use when you want to update your forms with results out of your thread.

A very simple demo below:

Code:
unit tcountunit;
//simple TThread demo by Glenn9999
interface

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

type
  TTCounter = class(TThread)
  private
    FCounter: Integer;
    FLabel1: TLabel;
  public
    constructor Create(Label1: TLabel);
  protected
    procedure UpdateStuff;
    procedure Execute; override;
  end;
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    TCounter: TTCounter;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

constructor TTCounter.Create(Label1: TLabel);
// constructor for the thread.  Sets local storage for the label, initializes and
// creates thread.
begin
  FLabel1 := Label1;
  FCounter := 0;
  inherited Create(false);
end;

procedure TTCounter.Execute;
// thread execution.  Increments counter and updates a label on main form.
begin
  FreeOnTerminate := True;
  while not Terminated do
    begin
      inc(FCounter);
      Synchronize(UpdateStuff);
      sleep(10);  // a delay is necessary so the program does not use a lot of CPU.
    end;
end;

procedure TTCounter.UpdateStuff;
// procedure called by Synchronize.  Must be parameterless.
  begin
    FLabel1.Caption := IntToStr(FCounter);
// you can also just directly address the form, too...
//  Form1.Label1.Caption := IntToStr(FCounter);
  end;

procedure TForm1.Button1Click(Sender: TObject);
// start counting thread.
begin
  // shut down original thread if we restart.
  if Assigned(TCounter) then
    TCounter.Terminate;
  TCounter := TTCounter.Create(Label1);
  Button2.Enabled := true;
  Button4.Enabled := true;
end;

procedure TForm1.Button2Click(Sender: TObject);
// pause counting thread, more to demo TThread.
begin
  Button2.Enabled := false;
  Button3.Enabled := true;
  TCounter.Suspend;
end;

procedure TForm1.Button3Click(Sender: TObject);
// resume counting thread, more to demo TThread.
begin
  Button2.Enabled := true;
  Button3.Enabled := false;
  TCounter.Resume;
end;

procedure TForm1.Button4Click(Sender: TObject);
// terminate counting thread, more to demo TThread.
begin
  Button2.Enabled := false;
  Button3.Enabled := false;
  Button4.Enabled := false;
  if Assigned(TCounter) then
    TCounter.Terminate;
end;

procedure TForm1.FormDestroy(Sender: TObject);
// you want to make sure your thread gets shut down before main program does.
begin
  if Assigned(TCounter) then
    TCounter.Terminate;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Button2.Enabled := false;
  Button3.Enabled := false;
  Button4.Enabled := false;
end;

end.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top