Android 和 Application.ProcessMessages
Android and Application.ProcessMessages
我有一个使用表单作为消息框的应用程序,
在此 "message box" 我 运行 线程中更改其上的消息
线程完成后,在消息框上我显示按钮,只有在单击按钮代码后才能继续
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 上它工作正常)
没有Application.ProcessMessages怎么办?
您不应该使用等待循环。因此,您根本不需要在任何平台上使用 ProcessMessages()
。
启动线程然后退出 OnClick
处理程序到 return 到主 UI 消息循环,然后让线程在需要时向主线程发出通知更新 UI。线程完成后,关闭窗体。
例如:
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;
我有一个使用表单作为消息框的应用程序, 在此 "message box" 我 运行 线程中更改其上的消息 线程完成后,在消息框上我显示按钮,只有在单击按钮代码后才能继续
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 上它工作正常)
没有Application.ProcessMessages怎么办?
您不应该使用等待循环。因此,您根本不需要在任何平台上使用 ProcessMessages()
。
启动线程然后退出 OnClick
处理程序到 return 到主 UI 消息循环,然后让线程在需要时向主线程发出通知更新 UI。线程完成后,关闭窗体。
例如:
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;