SPECIAL OFFER        Order and save right now!
20% off with the 729824315 dicscount code for Ultimate Pack and any another product for Delphi from Greatis Programming!

⤷ Synchronize thread with application

This is a simple example, which shows how to synchronize thread with application. In our example, we will get result of calculations in a main form.

Use for this Synchronize method of TThread type.


type
  TTestThread = class(TThread)
  private
    j: Integer;
  protected
    procedure GetResult;
    procedure Execute; override;
  end;

...

procedure TTestThread.GetResult;
begin
  Form1.Caption:=IntToStr(j);
end;

procedure TTestThread.Execute;
var
  i: Integer;
begin
  j:=1;
  FreeOnTerminate:=True;
  for i:=1 to 90000000 do
  begin
    if Terminated then break;
    Inc(j, Round(Abs(Sin(Sqrt(i)))));
  end;
  Synchronize(GetResult);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  NewThread: TTestThread;
begin
  NewThread:=TTestThread.Create(False);
end;
More for developers
Databases for Amazon Associates
Amazon Categories
Amazon Nodes