0

Delphi 10.4 FMX(虽然我确信这是一个一般的 Delphi 问题)

我的对话窗口正在读取一个大文件。

  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);

如果没有 Application.ProcessMessages,则永远不会绘制 Label 和 ProgressBar。我不认为 Application.ProcessMessages 是最好的方法,因为它在几千次调用后往往会崩溃。

在这样的批处理过程中重新绘制组件的最佳实践是什么?

4

1 回答 1

1

像这样的东西:

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);
于 2020-07-30T08:42:00.947 回答