如何在应用程序中避免闪烁?(How do I avoid flicker in my application?)

这是每个开发人员时不时面临的一个非常普遍的问题,当视觉更新可能如此迅速和快速以至于导致表单内容闪烁时。 我目前正在使用线程搜索文件并触发事件到其调用(主VCL)线程以报告每个搜索结果。 如果您曾经使用过FindFirst / FindNext ,或者执行任何大型循环来执行非常快速和快速的迭代,那么您会知道在每次小迭代中更新GUI都非常繁重,几乎无法实现线程,因为线程然后依赖于GUI可以更新的速度(在线程内的每次迭代)。

我正在对线程中的每个事件做的事情(在1毫秒内可能有100个事件),只是递增一个全局整数,来计算迭代次数。 然后,我在主窗体上的标签中显示该数字。 可以想象,线程的快速更新将导致闪烁超出控制范围。

所以我想知道的是,当一个线程以比它能够更新的速度更快地向它提供事件时,如何避免GUI中的这种快速闪烁?

注意:我使用的是VCL样式,因此闪烁变得更糟。

It's a very common problem every developer faces every now and then, when visual updates may be so rapid and fast that it causes the contents of the form to flicker. I'm currently using a thread to search files and trigger an event to its calling (main VCL) thread to report each and every search result. If you've ever used the FindFirst / FindNext, or done any large loop for that matter which performs very fast and rapid iterations, then you would know that updating the GUI on every little iteration is extremely heavy, and nearly defeats the purpose of a thread, because the thread then becomes dependent on how fast the GUI can update (on each and every iteration inside the thread).

What I'm doing upon every event from the thread (there could be 100 events in 1 millisecond), is simply incrementing a global integer, to count the number of iterations. Then, I am displaying that number in a label on the main form. As you can imagine, rapid updates from the thread will cause this to flicker beyond control.

So what I would like to know is how to avoid this rapid flicker in the GUI when a thread is feeding events to it faster than it's able to update?

NOTE: I am using VCL Styles, so the flicker becomes even worse.

最满意答案

这确实是一个常见的问题,并不总是由线程,而是由任何需要更新GUI的循环,同时循环迭代的速度比GUI能够更新的速度快。 快速简便的解决方案是使用Timer更新GUI。 每当循环触发更新时,不要立即更新GUI。 相反,为每个可能需要更新的事物(显示计数的标签)设置一些全局变量(如全局迭代计数),然后使计时器执行GUI更新。 将计时器的间隔设置为100-200毫秒。 这样,您可以控制GUI更新,使其仅在设置计时器间隔时频繁发生。

另一个优点是,线程的性能将不再取决于GUI的更新速度。 线程可以触发其事件并仅增加此整数,并继续其工作。 请记住,您仍然必须确保您的GUI具有线程保护功能。 这是我自己的艺术,我不会介绍并假设你已经知道。

注意:您需要执行的GUI更新越多,您可能需要调整计时器的间隔。

This is indeed a common problem, not always by threads, but by any loop which needs to update the GUI, and at the same time the loop is iterating faster than the GUI is able to update. The quick and easy solution to this is to use a Timer to update your GUI. Whenever the loop triggers an update, don't immediately update the GUI. Instead, set a some global variable (like the global iteration count) for each thing which may need to be updated (the label to display the count), and then make the timer do the GUI updates. Set the timer's interval for like 100-200 msec. This way, you control the GUI updates to only occur as frequent as you set the timer interval.

Another advantage to this is the performance of your thread will no longer depend on how fast your GUI can update. The thread can trigger its event and only increment this integer, and continue with its work. Keep in mind that you still must make sure you're thread-protecting your GUI. This is an art of its own which I will not cover and assume you already know.

NOTE: The more GUI updates you need to perform, the higher you may need to tweak the timer's interval.

更多推荐