Mike at Bookup
Mike at Bookup

Reputation: 1314

Delphi FMX painting components without Application.ProcessMessages

Delphi 10.4 FMX (although I'm sure this is a general Delphi question)

My dialog window is reading a large file.

  AssignFile(theFile, OpenDialog1.FileName);

  Reset(theFile);

  while not EOF(theFile) and not CancelButtonPressed do
    begin
      ReadLn(theFile, theLine);
      Label1.Text := theLine;
      ProgressBar1.Value := PercentageOfFileRead;

      // Application.ProcessMessages;
    end;

  CloseFile(theFile);

Without the Application.ProcessMessages, the Label and ProgressBar are never painted. I don't think Application.ProcessMessages is the best way to go though as it tends to crash after a few thousand calls.

What is the best practice for repainting components during a batch process like this?

Upvotes: 0

Views: 643

Answers (1)

HeartWare
HeartWare

Reputation: 8243

Something like this:

AssignFile(theFile, OpenDialog1.FileName);

Reset(theFile);

TThread.CreateAnonymousThread(PROCEDURE
                                BEGIN
                                  while not EOF(theFile) and not CancelButtonPressed do
                                  begin
                                    ReadLn(theFile, theLine);
                                    TThread.Synchronize(NIL,PROCEDURE
                                                              BEGIN
                                                                Label1.Text := theLine;
                                                                ProgressBar1.Value := PercentageOfFileRead;
                                                              END);
                                  end;
                                  CloseFile(theFile);
                                END);

Upvotes: 1

Related Questions