gpt4 book ai didi

delphi - 在 firemonkey 移动开发中正确使用 TAniIndicator 等待处理

转载 作者:行者123 更新时间:2023-12-03 15:16:25 29 4
gpt4 key购买 nike

我正在使用 Delphi XE-5(Firemonkey 移动应用程序)

我正在尝试通过在长时间处理过程中显示来使 TAniIndicator 正常工作。我的主窗体上有一个 TAniIndicator (AniIndi),但它不旋转。它显示正确,但不旋转。

begin
Loading:= True;
AniIndi.Visible:= True;
AniIndi.Enabled:= True;
UpdateAll;
Application.ProcessMessages;

//do my processsing here

Loading:= False;
AniIndi.Enabled:= False;
AniIndi.Visible:= False;
UpdateAll;
Application.ProcessMessages;
end;

//根据雷米的答案进行编辑

TLoadThread = class(TThread)
public
Config: Boolean;
constructor Create(const aConfig: Boolean); reintroduce;
protected
procedure DoProcessing;
procedure Execute; Override;
end;

var
loading: Boolean = false;
zLThread: TLoadThread = nil;

constructor TLoadThread.Create(const aConfig: Boolean);
begin
inherited Create(true);
Config:= aConfig;
end;

procedure TLoadThread.DoProcessing;
var
begin
//do processing here and update main form
end;

procedure TLoadThread.Execute;
begin
FreeOnTerminate:= true;
Synchronize(DoProcessing);
end;


procedure TfrmMain.FormActivate(Sender: TObject);
begin
zLThread:= TLoadThread.Create(True, Host, NamePath, Config, Port);
zLThread.OnTerminate := ThreadTerminated;
zLThread.Start;
Loading := True;
AniIndi.Visible := True;
AniIndi.Enabled := True;
UpdateAll;
end;

procedure TfrmMain.ThreadTerminated(Sender: TObject);
begin
zLThread := nil;
Loading := False;
AniIndi.Enabled := False;
AniIndi.Visible := False;
UpdateAll;
end;

最佳答案

当您的长进程运行时,您的主线程需要保持对消息队列的响应。如果没有,您将阻止动画(以及 UI 的其他方面)接收新消息,例如绘制请求和计时器通知。您需要将所有长处理移至单独的线程。启动线程,然后启动动画。同时让主线程正常处理UI。当线程完成时,让它通知主线程,然后主线程可以停止动画,并完成对线程结果所需的任何其他处理(如果有)。例如:

type
TLoadThread = class(TThread)
public
Host: string;
NamePath: string;
Port: Integer;
Config: Boolean;
constructor Create(const aHost, aNamePath: string; aPort: Integer; aConfig: Boolean); reintroduce;
protected
procedure Execute; override;
end;

constructor TLoadThread.Create(const aHost, aNamePath: string; aPort: Integer; aConfig: Boolean);
begin
inherited Create(True);
FreeOnTerminate := True;
Host := aHost;
NamePath := aNamePath;
Port := aPort;
Config := aConfig;
end;

procedure TLoadThread.Execute;
begin
//do processing

Synchronize(
procedure
//update main form
end
);

//do processing
end;

var
Loading: Boolean = False;
zLThread: TLoadThread = nil;

procedure TfrmMain.FormActivate(Sender: TObject);
begin
zLThread := TLoadThread.Create(Host, NamePath, Port, Config);
zLThread.OnTerminate := ThreadTerminated;
zLThread.Start;
Loading := True;
AniIndi.Visible := True;
AniIndi.Enabled := True;
UpdateAll;
end;

procedure TfrmMain.ThreadTerminated(Sender: TObject);
begin
zLThread := nil;
Loading := False;
AniIndi.Enabled := False;
AniIndi.Visible := False;
UpdateAll;
end;

关于delphi - 在 firemonkey 移动开发中正确使用 TAniIndicator 等待处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20688839/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com