简体   繁体   English

Delphi FMX - HTTPS POST 带 AniIndicator

[英]Delphi FMX - HTTPS POST with AniIndicator

On Android, when I touch the screen during a Indy https post function "Application isn't responding" appears.在 Android 上,当我在 Indy https 发布 function 期间触摸屏幕时出现“应用程序没有响应”。 I don't want to see it.我不想看到它。 I want to show AniIndicator animation during loading data without main thread get busy.我想在加载数据期间显示 AniIndicator animation 而没有主线程忙。

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.您正在将TIdHTTP.Post()操作Synchronize()到主线程,这将阻塞您的 UI,直到 HTTP 操作完成。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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