Android和Application.ProcessMessages

我有使用表格作为消息框的应用程序, 在此“消息框”中,我运行更改消息的线程 并且在线程完成后,在消息框上我会显示按钮,只有单击按钮代码后才能继续

var
  FStart: TFStart;
  VariableX:Boolean;

implementation

uses UApp,UMess;
{$R *.fmx}

procedure TFStart.Button2Click(Sender: TObject);
begin
  VariableX:=false;
  {
    There i show window and start thread
    after finish thread set VariableX as true
    and close form
  }
  // There i need to wait until thread finish 
  while VariableX = false do Application.ProcessMessages;
  {
    there i will continue to work with data returned by thread
  }
end;

我知道Marco Cantu说使用Application.ProcessMessages不是一个好主意 就我而言,应用程序以sigterm停止(在Windows和ios上运行正常)

在没有Application.ProcessMessages的情况下如何做?

wdjylove 回答:Android和Application.ProcessMessages

您不应使用等待循环。因此,您完全不需要在任何平台上使用ProcessMessages()

启动线程,然后退出OnClick处理程序以返回主UI消息循环,然后在需要更新UI时使线程向主线程发出通知。线程完成后,关闭窗体。

例如:

procedure TFStart.Button2Click(Sender: TObject);
var
  Thread: TThread;
begin
  Button2.Enabled := False;
  Thread := TThread.CreateAnonymousThread(
    procedure
    begin
      // do threaded work here...
      // use TThread.Synchronize() or TThread.Queue()
      // to update UI as needed...
    end
  );
  Thread.OnTerminate := ThreadDone;
  Thread.Start;
end;

procedure TFStart.ThreadDone(Sender: TObject);
begin
  Close;
end;
本文链接:https://www.f2er.com/3134514.html

大家都在问