Android and Application.ProcessMessages

*爱你&永不变心* 提交于 2021-02-19 06:34:05

问题


i have application where I'm using form as message box, in this "message box" i run thread that changing messages on it and after thread finish, on message box i show buttons, only after clicking on button code can continue

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;

I know that Marco Cantu say that its not good idea to use Application.ProcessMessages In my case application stop with sigterm (On windows and ios its working good)

How to do it without Application.ProcessMessages?


回答1:


You should not be using a wait loop. Thus you would not need to use ProcessMessages() at all, on any platform.

Start the thread and then exit the OnClick handler to return to the main UI message loop, and then have the thread issue notifications to the main thread when it needs to update the UI. When the thread is done, close the Form.

For example:

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://stackoverflow.com/questions/58775196/android-and-application-processmessages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!