[SOLVED] Memo is NOT cleared without Application.ProcessMessages; lazarus / 31-LCL ---------------------------------------------------------------------- CM630 · 2025-02-04 08:54 Just to make sure that this behaviour is correct. If I execute the code once it behaves properly. The second and the next time (after pressing Button1) it looks... spectacular (the text is not cleared, only the currently updated line is) If I put Application.ProcessMessages; after Memo1.Clear; it works as expected every time. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  i: integer;begin Memo1.Clear;// Application.ProcessMessages; for i:=0 to 10 do begin   Memo1.Append(IntToStr(i))  ;  sleep(1000); end; //forend; ---------------------------------------------------------------------- cdbc · 2025-02-04 09:12 Hi Your cpu goes from clearing the memo, straight to updating it with new data, before it has time to check its messagequeue, i.e.: 'GetMessage()' has lower priority than your loop-code. Therefore, the rules of thumb are: 1) Main thread takes care of GUI-stuff ...and not much else. 2) Any(and all) 'Heavy Lifting', should be put in a background thread. 3) If your house is on fire, you can briefly use 'ProcessMessages'! 4) 'Decoupling' is the name of the game. HTH Regards Benny ---------------------------------------------------------------------- Thaddy · 2025-02-04 09:37 memo.lines.text :=''; ? but even that requires a repaint. In both cases the actual memo content is empty. It is just that the memo's visual representation needs to be updated. You can also do that like so: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   memo1.lines.beginupdate;// that is a paint lock  memo1.lines.clear;  memo1.lines.endupdate; // forces a repaintThat option does not need processmessages. And is faster. ---------------------------------------------------------------------- rvk · 2025-02-04 09:41 Quote from: Thaddy on February 04, 2025, 10:37:45 ammemo.lines.text :=''; ? but even that requires a repaint. In both cases the actual memo content is empty. It is just that the memo's visual representation needs to be updated. You can also do that like so: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   memo1.beginupdate;// that is a paint lock  memo1.clear;  memo1.endupdate; // forces a repaintThat option does not need processmessages. And is faster. You probably mean: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   memo1.lines.beginupdate;// that is a paint lock  memo1.clear;  memo1.lines.endupdate;But NO, that doesn't update the screen !!!! Did you test this yourself? Apperantly not because you would have seen your code doesn't compile ---------------------------------------------------------------------- Thaddy · 2025-02-04 10:14 corrected it. And here it updates the screen. You can also call memo1.invalidate if it does not work (I am on trunks) It is really not necessary to call processmessages all the time. Invalidate simply registers a request to update at the next paint. And note the memo is cleared, it is just not updated on screen. ---------------------------------------------------------------------- rvk · 2025-02-04 10:19 Quote from: Thaddy on February 04, 2025, 11:14:02 amcorrected it. And here it updates the screen. You can also call invalidate if it does not work (I am on trunks) NO IT DOESN'T  (I'm also on trunk) Try it yourself. (or you are missing the point. You need to press the button a second time and you see the memo isn't cleared until the loop is done (and during the loop you only see the current line updates/blanked). Invalidate also doesn't update the screen. It sets a flag to update the screen the next time the cycle events comes around. Not directly. ---------------------------------------------------------------------- Thaddy · 2025-02-04 10:21 Well, after your comment I already did. The memo is cleared. and a repaint occurred. I only moved the silly sleep() outside the begin/endupdate. Sleep is blocking. ---------------------------------------------------------------------- rvk · 2025-02-04 10:28 Quote from: Thaddy on February 04, 2025, 11:21:11 amWell, after your comment I already did. The memo is cleared. and a repaint occurred. Here it doesn't. Maybe you should put that sleep(1000) back in that you probably removed. And you should know that invalidate isn't executed directly but only when the procedure ends. ---------------------------------------------------------------------- Thaddy · 2025-02-04 10:30 Sleep is blocking. If you don't want that use a thread. ---------------------------------------------------------------------- rvk · 2025-02-04 10:31 Quote from: Thaddy on February 04, 2025, 11:30:13 amSleep is blocking. If you don't want that use a thread. That wasn't the point. The point was that TMemo.Clear doesn't directly update the screen. You said EndUpdate and Invalidate it DOES update the screen. IT DOESN"T. It only updates the screen when the procedure ends end the rest of the events can be handled. If you are of a different opinion... please provide some code we can test to show invalidate does directly update the screen. ---------------------------------------------------------------------- BrunoK · 2025-02-04 11:15 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  Memo1.Clear;  Memo1.Refresh; ---------------------------------------------------------------------- rvk · 2025-02-04 11:20 Quote from: BrunoK on February 04, 2025, 12:15:49 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  Memo1.Clear;  Memo1.Refresh; Yes, TControl.Refresh (which calls TControl.Repaint) does paint the component directly (it's the correct call there). All other, like Invalidate and EndUpdate only signals the framework to do it after the current procedure but TControl.Repaint and TControl.Refresh repaints it directly. ---------------------------------------------------------------------- MarkMLl · 2025-02-04 11:21 Quote from: rvk on February 04, 2025, 11:31:41 amQuote from: Thaddy on February 04, 2025, 11:30:13 amSleep is blocking. If you don't want that use a thread. That wasn't the point. The point was that TMemo.Clear doesn't directly update the screen. You said EndUpdate and Invalidate it DOES update the screen. IT DOESN"T. It only updates the screen when the procedure ends end the rest of the events can be handled. If you are of a different opinion... please provide some code we can test to show invalidate does directly update the screen. That might to some extent depend on the widgetset, i.e. depending on how much of the functionality is provided by the interface code and how much by some Windows-style standard component. However I think that my experience at https://forum.lazarus.freepascal .org/index.php/topic,67337.msg519191.html#msg519191 might be relevant, which emphasised the importance of the begin/endupdate calls which was more or less where Thaddy came in. MarkMLl ---------------------------------------------------------------------- CM630 · 2025-02-04 11:27 I tried Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   memo1.lines.beginupdate;// that is a paint lock  memo1.lines.clear;  memo1.lines.endupdate; // forces a repaintIt changes nothing for me. I also tried: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Memo1.Clear;  Memo1.Refresh; It seems to do fine. About the “silly sleep” - I have observed this behaviour in two other cases (once in loading files and once when getting data from a DLL). I made a snippet with sleep, which visibly behaves the same way as the other cases. ---------------------------------------------------------------------- Martin_fr · 2025-02-04 11:44 Normally painting (screen updates...) is only done during message processing. That is either - when your code has run, and no other events are pending - when you call Application.ProcessMessages "no other events are pending" means, that even if your ButtonClick has finished, if there is already another click, or a timer, or any other event, then (usually / in most cases) that other event has higher priority than the paint event. The paint event usually waits. There may be occurrences where - by chance - a control will do paint/screen-update outside of the above schedule. I.e. immediately. But afaik those aren't guaranteed.  And maybe aren't even supposed to happen. Other than this "Repaint" should work immediately. Not checked, but "Refresh" may include "Repaint". ---------------------------------------------------------------------- CM630 · 2025-02-04 12:08 Quote from: Martin_fr on February 04, 2025, 12:44:00 pm... Not checked, but "Refresh" may include "Repaint". As I was told once “You will touch down in Beijing. It is very close to Peking.”