我想通过在长时间处理期间显示来让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;
//基于REMY的答案进行编辑
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;varbegin //do processing here and update main formend;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 processingend;
var Loading: Boolean = False; zLThread: TLoadThread = nil;procedure TfrmMain.FormActivate(Sender: TObject);begin zLThread := TLoadThread.Create(Host,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等待处理全部内容,希望文章能够帮你解决delphi – 在firemonkey移动开发中正确使用TAniIndicator等待处理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)