Author Topic: [SOLVED] TSQLQuery AfterScroll Defer Execution  (Read 643 times)

Fred Pelkim

  • Newbie
  • Posts: 2
[SOLVED] TSQLQuery AfterScroll Defer Execution
« on: May 16, 2021, 12:26:09 pm »
Hello there!

For a small personal project I created an application that basically displays a SQLite database with a few bells and whistles. I was happy with how it worked and performed until I realized that one specific element of the application seems to be quite slow on lower end systems:

My database contains a column which contains relative paths to image files.


I use a TImage to display the image files from the mentioned column and update the image file through AfterScroll:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SQLQuery1AfterScroll();
  2. begin
  3.   cover:=DBGrid1.DataSource.DataSet.Fields[2].value;
  4.   coverImage.Picture.LoadFromFile(cover.ToString());
  5. end

On a modern system scrolling through the database is fluent but on an older system it's sluggish. I guess that's because scrolling through the database goes through every row and executes the AfterScroll procedure every time. So if I were to quickly scroll through 10 rows of the database it would execute the procedure 10 times. Now I'm looking for a way to solve this problem. Maybe there's a way to "defer" the execution of the AfterScroll procedure to only run after a row is active for more than 500ms or something like that?

I'd be very thankful for tips on how to improve performance here.

« Last Edit: May 16, 2021, 01:10:30 pm by Fred Pelkim »


  • Hero Member
  • *****
  • Posts: 4914
Re: TSQLQuery AfterScroll Defer Execution
« Reply #1 on: May 16, 2021, 12:32:35 pm »
use a timer for that. while scrolling reset the timer to the start at the start of a scroll..

when the timer expires use its event to update the image and ensure that you cancel the timer while in there, the timer should only be enabled on the initial start of a scroll and disabled after it has triggered once to the event.

 You could also use the mouse calls and keyboard calls to determine if user is holding buttons but this may not work so well with touch screens
The only true wisdom is knowing you know nothing

Fred Pelkim

  • Newbie
  • Posts: 2
Re: TSQLQuery AfterScroll Defer Execution
« Reply #2 on: May 16, 2021, 01:09:28 pm »
Thanks a lot for the quick reply, jamie!

I added a TTimer with an Interval of 500 ms and used my old code to update the image in the timer's OnTimer event. Now the AfterScroll procedure just resets the TTimer. It works very well! Exactly what I was looking for!

Thanks again!



TinyPortal © 2005-2018