简体   繁体   中英

Delphi FMX - HTTPS POST with AniIndicator

On Android, when I touch the screen during a Indy https post function "Application isn't responding" appears. I don't want to see it. I want to show AniIndicator animation during loading data without main thread get busy.

my code:

function TFormMain.LoginChecker(iphttp: TIdHTTP): Boolean;
var
  s: string;
  Thread1: TThread;
begin
  Thread1 := TThread.CreateAnonymousThread
    (
    procedure
    begin

      TThread.Synchronize(Thread1,
        procedure
        begin
          s := iphttp.Post(ServerRoot + 'lc.php', TStream(nil));
        end);
    end);
  Thread1.Start;
  if Thread1.Finished then
  begin
    try
      if s = '1' then
        Result := True
      else
        Result := False;
    except
      Result := False;
    end;
  end;
end;

procedure TFormMain.Button2Click(Sender: TObject);
var
  Thread1: TThread;
  Logined: Boolean;
begin
  try
    AniIndicator1.Visible := True;
    AniIndicator1.Enabled := True;
    TabControl1.Visible := False;

    Logined:= LoginChecker(IdHTTP1);
    if Logined then
      ShowMessage('Yes!')
    else
      ShowMessage('No');
  finally
    AniIndicator1.Visible := False;
    AniIndicator1.Enabled := False;
    TabControl1.Visible := True;
  end; 

You are Synchronize() 'ing the TIdHTTP.Post() operation to the main thread, which will block your UI until the HTTP operation is finished. Don't do that. The whole point of creating a worker thread is to run code in another thread . So let the thread run normally, and notify the main thread when there is something worth reporting.

Try something more like this instead:

function TFormMain.BeginLogin;
begin
  AniIndicator1.Visible := True;
  AniIndicator1.Enabled := True;
  TabControl1.Visible := False;

  TThread.CreateAnonymousThread(
    procedure
    var
      Logined: Boolean;
    begin
      Logined := False;
      try
        Logined := (IdHTTP1.Post(ServerRoot + 'lc.php', TStream(nil)) = '1');
      finally
        TThread.Queue(nil,
          procedure
          begin
            LoginFinished(Logined);
          end
        );
      end;
    end
  ).Start;
end;

procedure TFormMain.LoginFinished(Logined: Boolean);
begin
  if Logined then
    ShowMessage('Yes!')
  else
    ShowMessage('No');

  AniIndicator1.Visible := False;
  AniIndicator1.Enabled := False;
  TabControl1.Visible := True;
end; 

procedure TFormMain.Button2Click(Sender: TObject);
begin
  BeginLogin;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM