delphi - 没有 Application.ProcessMessages 的 Delphi FMX 绘制组件
问题描述
Delphi 10.4 FMX(虽然我确信这是一个一般的 Delphi 问题)
我的对话窗口正在读取一个大文件。
AssignFile(theFile, OpenDialog1.FileName);
Reset(theFile);
while not EOF(theFile) and not CancelButtonPressed do
begin
ReadLn(theFile, theLine);
Label1.Text := theLine;
ProgressBar1.Value := PercentageOfFileRead;
// Application.ProcessMessages;
end;
CloseFile(theFile);
如果没有 Application.ProcessMessages,则永远不会绘制 Label 和 ProgressBar。我不认为 Application.ProcessMessages 是最好的方法,因为它在几千次调用后往往会崩溃。
在这样的批处理过程中重新绘制组件的最佳实践是什么?
解决方案
像这样的东西:
AssignFile(theFile, OpenDialog1.FileName);
Reset(theFile);
TThread.CreateAnonymousThread(PROCEDURE
BEGIN
while not EOF(theFile) and not CancelButtonPressed do
begin
ReadLn(theFile, theLine);
TThread.Synchronize(NIL,PROCEDURE
BEGIN
Label1.Text := theLine;
ProgressBar1.Value := PercentageOfFileRead;
END);
end;
CloseFile(theFile);
END);
推荐阅读
- python - 如何让python等到内存空闲然后执行下一段代码?
- python - 如何使用 asyncio 从使用 SubprocessProtocol 的子进程中读取并在任意点终止该子进程?
- swift5 - Java to swift conversion in cryptography
- php - 有没有办法把图像放在一起?
- javascript - 我如何计算计算已经运行了多少次
- selenium-webdriver - 将 Selenium WebDriver 代码转换为 JMeter WebDriver Sampler 代码
- postgresql - 将数据从 redshift 传输到 postgresql
- go - 如何全局访问处理程序值
- python - 训练 GAN 时批次中的所有图像都相同
- python - 从 Python 应用程序打开 Windows 相机