Search code examples
androiddelphifiremonkey

Android and Application.ProcessMessages


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?


Solution

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