簡體   English   中英

Delphi FMX - HTTPS POST 帶 AniIndicator

[英]Delphi FMX - HTTPS POST with AniIndicator

在 Android 上,當我在 Indy https 發布 function 期間觸摸屏幕時出現“應用程序沒有響應”。 我不想看到它。 我想在加載數據期間顯示 AniIndicator animation 而沒有主線程忙。

我的代碼:

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; 

您正在將TIdHTTP.Post()操作Synchronize()到主線程,這將阻塞您的 UI,直到 HTTP 操作完成。 不要那樣做。 創建工作線程的全部意義在於在另一個線程中運行代碼 所以讓線程正常運行,當有值得報告的事情時通知主線程。

嘗試更多類似的東西:

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