Search code examples
multithreadingdelphidelphi-2010vcldelphi-xe4

How to update GUI controls with versions of Delphi (Pre Delphi 2010) without Synchronize


I have some apps built with Delphi 2010 and XE4 that use Synchronize in a thread. I think Synchronize was introduced to Delphi in Delphi 2010. My thread operates very well so that is not the problem.

My question is: Is there any way to "Synchronize" with versions of Delphi prior to Delphi 2010 or to ask it in a different way, how do you update GUI controls in these earlier versions of Delphi without Synchronize?

The code shown below is a subset of the actual code to reduce the length of this post.

type
  { A TThread descendent for loading Images from a folder }
  TFolderLoadingThread = class(TThread)
  private
    { Private declarations }
    AspectRatio: double;
  protected
    { Protected declarations }
    procedure Execute; override;
  public
    { Public declarations }
    constructor Create(CreateSuspended: Boolean);
  end;

procedure TFolderLoadingThread.Execute;
{ Load images ImageEnView in the thread. }
begin
  inherited;
  { Free the thread onTerminate }
  FreeOnTerminate := True;
  if not Terminated then
  begin
    { Set the Progressbar.Max Value }
    **Synchronize**(
      procedure
      begin
         if iFileCount > 0 then
          Form1.Gauge1.MaxValue := iFileCount - 1;
 end);
end;

Solution

  • Synchronize is very old routine, but there are no anonymous procedures in Delphi versions prior to D2009. Synchronize was intended to call a method without parameters in these versions.

    procedure TFolderLoadingThread.UpdateProgress;
    begin
     if iFileCount > 0 then
              Form1.Gauge1.MaxValue := iFileCount - 1;
    end;
    

    in Execute:

    do thead work...
    Synchronize(UpdateProgress);
    

    P.S. You have not to call Terminate in the Execute body