Mega Code Archive

 
Categories / Delphi / Examples
 

Synchronize thread with application

Title: 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;