android - Android 和 Application.ProcessMessages
问题描述
我有应用程序,我将表单用作消息框,在这个“消息框”中,我运行线程来更改消息,线程完成后,在消息框上我显示按钮,只有在单击按钮代码后才能继续
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
处理程序以返回主 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;
推荐阅读
- java - 如何使用Apache POI从excel(xlsx)文件中读取整数单元格值作为字符串而不进行任何更改
- python - 图像显示和标签代码仅显示:
- javascript - 如何使用延迟 5 秒的 for 循环删除 div
- javascript - FullCalendar: - 在哪里编辑尺寸?
- django - Django slugs 不是唯一的 unique=True
- python - 多处理 pickle.load() :: EOFError - 输入不足
- templates - 在 golang gin 简单模板示例中,如何呈现不带引号的字符串?
- python - 如何修复我的动态进度条,以便为它制作可自定义的长度,并且它仍然用正确数量的字符填充进度条?
- git - 列出合并分支的 Git 命令
- php - 如何将用户数据从 LoginController 传递到我的 UserController