问题描述
我有使用表格作为消息框的应用程序,在此消息框"中,我运行更改消息的线程并且在线程完成后,在消息框上我会显示按钮,只有在单击按钮代码后,才能继续
i have application where I'm using form as message box,in this "message box" i run thread that changing messages on itand after thread finish, on message box i show buttons, only after clicking on button code can continue
var
FStart: TFStart;
VariableX:Boolean;
implementation
uses UApp,UMess;
{$R *.fmx}
procedure TFStart.Button2Click(Sender: TObject);
begin
VariableX:=false;
{
There i show window and start thread
after finish thread set VariableX as true
and close form
}
// There i need to wait until thread finish
while VariableX = false do Application.ProcessMessages;
{
there i will continue to work with data returned by thread
}
end;
我知道Marco Cantu说使用Application.ProcessMessages不是一个好主意就我而言,应用程序以sigterm停止(在Windows和ios上运行正常)
I know that Marco Cantu say that its not good idea to use Application.ProcessMessagesIn my case application stop with sigterm (On windows and ios its working good)
在没有Application.ProcessMessages的情况下如何做?
How to do it without Application.ProcessMessages?
推荐答案
您不应使用等待循环.因此,您完全不需要在任何平台上都使用 ProcessMessages()
.
You should not be using a wait loop. Thus you would not need to use ProcessMessages()
at all, on any platform.
启动线程,然后退出 OnClick
处理程序以返回主UI消息循环,然后在需要更新UI时使线程向主线程发出通知.线程完成后,关闭窗体.
Start the thread and then exit the OnClick
handler to return to the main UI message loop, and then have the thread issue notifications to the main thread when it needs to update the UI. When the thread is done, close the Form.
例如:
procedure TFStart.Button2Click(Sender: TObject);
var
Thread: TThread;
begin
Button2.Enabled := False;
Thread := TThread.CreateAnonymousThread(
procedure
begin
// do threaded work here...
// use TThread.Synchronize() or TThread.Queue()
// to update UI as needed...
end
);
Thread.OnTerminate := ThreadDone;
Thread.Start;
end;
procedure TFStart.ThreadDone(Sender: TObject);
begin
Close;
end;
这篇关于Android和Application.ProcessMessages的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!