加快Delphi状态栏的更新速度
在 Delphi 中更新状态栏需要很长时间!
示例:我搜索文件并在状态栏中显示找到和搜索的文件数:
OwnerForm.StatusBar1.SimpleText
:= Format('Searching (%d found in %d files) ...', [NumFound, Total]);
状态栏每更新 200 次,搜索时间就会增加大约 1 秒。
有没有什么方法可以减少这种过多的开销,但仍然更新用户的状态?
Updating the status bar in Delphi takes so darn long!
Example: I search for files and display the number of files found and searched in the status bar:
OwnerForm.StatusBar1.SimpleText
:= Format('Searching (%d found in %d files) ...', [NumFound, Total]);
This adds approximately 1 second of time to the search for every 200 times the status bar is updated.
Are there any ways to reduce this excessive overhead, but still update the status for the user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要这么快更新状态栏。如果您更新状态如此频繁以至于对您的性能产生严重影响,用户将如何读取状态?
此外,我进行了一个小测试,结果显示它在 100 多毫秒内更新了状态栏 1000 次。这是我用了 5 年的廉价家用电脑上的。
[编辑]
替代解决方案:
Don't update your status bar this fast. How will a user be able to read the status if you update it so often that it has a serious impact on your performance?
Besides I ran a little test that shows it updates the status bar a 1000 times in just over 100 ms. This is on my 5 years old cheap home pc.
[edit]
Alternative solution:
当您在单独的线程中运行搜索时,您可以简单地使用 NumFound 和 Total 值更新一些变量。在主线程中,您可以每秒触发一次(或您喜欢的任何更新间隔)来读取此变量并更新状态栏。
由于 NumFound 和 Total 可能是整数,因此您可以使用 InterlockedXXX 函数以简单但线程安全的方式更新变量。
When you are running the search in a separate thread, you can simply update some variables with the NumFound and Total value. In the main thread you can fire a time every second (or whatever update interval you prefer) that reads this variables and updates the status bar.
As NumFound and Total probably are Integers, you can use the InterlockedXXX functions to update the variables in a simple but threadsafe way.