Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Minimize Overhead when Processing Messages in a Long Loop

I've got some long but simple loops in my Delphi program that may loop millions of times and take some seconds to execute. The code inside of the loop is very fast and has been optimized. It just takes long because it is done so many times.

e.g.:

Screen.Cursor = crHourGlass;
R := FirstRecord;
while R <> nil do begin
  { do something simple with R.Value }
  R := R.NextRecord;
end;
Screen.Cursor := crDefault;

Now I don't want my program to be non-responsive, so I want to add an Application.ProcessMessages inside the loop. But I also want the added statements to slow down my loop as little as possible.

I am following a linked list, so I don't even have a counting variable available and would have to add one if I wanted intervals. Or I'd have to add a timer, but need to minimize the time checking.

How should I implement this to minimize the overhead that's added?


Conclusion:

For now, I'm doing something like APZ28's answer.

But it looks like long term I should implement some sort of threading to handle this. Thanks for pointing this out, because I thought that Application.ProcessMessages was the only way to do it.

like image 450
lkessler Avatar asked Dec 03 '22 11:12

lkessler


1 Answers

Can you put the work loop in a thread, freeing up the main thread for the GUI loop processing.

like image 109
Gregor Brandt Avatar answered Jan 08 '23 10:01

Gregor Brandt