How can I avoid flickering in my application?

This is a very common problem that every developer faces from time to time when visual updates can be so fast and fast that it causes form content to flicker. I am currently using a thread to search for files and fire an event on its calling thread (main VCL) to report each search result. If you've ever used FindFirst

/ FindNext

or done any big loop that does very fast and fast iterations, then you know that updating the GUI on every small iteration is extremely hard and almost defeats the purpose of the thread, because the thread then becomes dependent on how fast the GUI can be updated (at each iteration within the thread).

What am I doing on every event from the thread (maybe 100 events in 1 millisecond) just incrementing the global integer to count the number of iterations. Then I show this number in a label on the main form. As you can imagine, fast updates from the stream will make this flicker unchecked.

So what I would like to know is how to avoid this fast flickering in the GUI when the thread is feeding events to it faster than it can update?

NOTE. I am using VCL Styles so the flickering gets worse.

+3


source to share


1 answer


This is really a common problem, not always on threads, but on any loop that needs to update the GUI, and at the same time, the loop is iterating faster than the GUI can update. A quick and easy solution to this is to use a timer to refresh your GUI. Whenever a loop starts an update, don't immediately update the GUI. Instead, set some global variable (like a global iteration counter) for every thing that might need to be updated (shortcut to display the counter) and then make the timer GUI updates. Set the timer interval as 100-200ms. This way, you only monitor the GUI updates as often as you set the timer interval.

Another benefit of this is that your thread performance will no longer be affected by how quickly your GUI can update. The thread can initiate its event and only increase this integer and continue its work. Keep in mind that you should still make sure that you are protecting the thread from your GUI. This is my own art, which I will not tell and assume that you already know.



NOTE. The more GUI updates you need to make, the higher you may need to adjust the timer interval.

+4


source







All Articles