Recent

Author Topic: Secondary form does not refresh  (Read 1862 times)

wschulte

  • New Member
  • *
  • Posts: 39
Secondary form does not refresh
« on: March 08, 2024, 05:50:46 am »
 When I open a secondary form, write some lines to a memo on it and then start a lengthy operation, the secondary form show up only AFTER that lengthy operation ends. That is despite processmessages calls just before the operation.
In the attachment I made an example. If I click [Process] the message is displayed on the memo, the operation is performed, and the end message is written. But if I press [To form] the secondary form shows up all grey and displays all messages AFTER the operation ended. That is not what I want: I want the start message to be visible BEFORE the operation starts.

What am I missing here ?

Lazarus 3.0 (2024-02-28), FreePascal 3.2.2 on Linux mint 21.1
« Last Edit: March 08, 2024, 05:55:14 am by wschulte »

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Secondary form does not refresh
« Reply #1 on: March 08, 2024, 06:35:30 am »
When I open a secondary form, write some lines to a memo on it and then start a lengthy operation, the secondary form show up only AFTER that lengthy operation ends. That is despite processmessages calls just before the operation.
processmessages should also be called during (and inside) the lengthy process.

Quote
In the attachment ...
Unfortunately, no attachment.

Quote
What am I missing here ?
Perhaps the attachment, just like me ?  ;D

PS: if you are unable to attach your project then you can obtain information here on how to do that
« Last Edit: March 08, 2024, 06:44:25 am by TRon »
Today is tomorrow's yesterday.

wschulte

  • New Member
  • *
  • Posts: 39
Re: Secondary form does not refresh
« Reply #2 on: March 08, 2024, 08:32:22 am »
Thanks for the reply. The attachment .. oh my, I forgot it  :-[, but not this time.

The problem is that in the case I stumbled on this, the lengthy process is out of my hands: the creation of a TCSVDocument. This is in this case a very lengthy process (as in 30 seconds and more) and I am heitant to change the component.

Mind you, in the test case I just do a stupid loop. But it shows the behavior I see,
« Last Edit: March 08, 2024, 08:33:59 am by wschulte »

cdbc

  • Hero Member
  • *****
  • Posts: 2816
    • http://www.cdbc.dk
Re: Secondary form does not refresh
« Reply #3 on: March 08, 2024, 08:44:17 am »
Hi
Would it be possible for you to call the /lengthy operation/ in an anonymous thread?!?
That way, you'd have your hands(read forms) free to do other things...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

wschulte

  • New Member
  • *
  • Posts: 39
Re: Secondary form does not refresh
« Reply #4 on: March 08, 2024, 08:55:17 am »
Quote
cdbc
« Reply #3 on: Today at 08:44:17 am »
Would it be possible for you to call the /lengthy operation/ in an anonymous thread?!?

I have been thinking about that. It is quite possible, but I am not so well known with threads. I think I will have to solve it that way.

I assume you mean with an anonymous thread just a TThread object .. ?

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Secondary form does not refresh
« Reply #5 on: March 08, 2024, 08:56:46 am »
First change this:
Code: Pascal  [Select][+][-]
  1. while (index >= 0) do
  2.     index := index -1;
  3.  
into this
Code: Pascal  [Select][+][-]
  1.   while (index >= 0) do
  2.   begin
  3.     index := index -1;
  4.     Application.ProcessMessages;
  5.   end;
  6.  
for both loops

Then realize that you wait for a key to be pressed on form2.... but what component is actually active ? indeed, the memo. So use the memo's keypress event  ;D

That will at least make your program more responsive. You can worry about details later.

And yes, as suggested by cdbc when there is no other way then using a thread comes to mind.
Today is tomorrow's yesterday.

wschulte

  • New Member
  • *
  • Posts: 39
Re: Secondary form does not refresh
« Reply #6 on: March 08, 2024, 09:16:39 am »
Quote
In replay #5 TRon wrote:
Code: Pascal
  while (index >= 0) do
  begin
    index := index -1;
    Application.ProcessMessages;
  end;
 
for both loops
The first loop (that is under the button [Process]) doesn't need the ProcessMessages: it displays the start anyway before the loop starts. That's what puzzles me, as it doesn't do so in the second loop (under the [To form] button).
In the original case I haven't any control over the loop (opening a large csv-document with TCSVDocument).

For now I will follow cdbc's suggestion to do it in a thread.

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Secondary form does not refresh
« Reply #7 on: March 08, 2024, 09:37:06 am »
In the original case I haven't any control over the loop (opening a large csv-document with TCSVDocument).
Ah ok. Yes, in that case better use a thread as suggested by cdbc.

It is not that difficult to use threads, see for example wiki here. If you do not need to do anything special (other than loading a document) then it is fairly easy as f.e. you can use your main program thread to display a timer/busy signal to the user during loading.

Strike that, User KodeZwerg hinted me in another thread related thread  :) to have a look at TThread class... I did not know that the class was extended to have cool things like this. Plain and simple  :)
« Last Edit: March 08, 2024, 09:54:51 am by TRon »
Today is tomorrow's yesterday.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Secondary form does not refresh
« Reply #8 on: March 08, 2024, 10:02:25 am »
One word of warning here. Be very careful about scattering Application.ProcessMessages throughout the main thread of a program in an attempt to keep things running, and also putting Synchronize(shim) in a background thread so that e.g. a progress bar is updated.

If the shim calls something with an A.PM in it, you've got a potential recursion which can be somewhat difficult to track down if you're not alert to the possibility.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Secondary form does not refresh
« Reply #9 on: March 08, 2024, 09:41:40 pm »
One word of warning here.
Yes indeed MarkMLI. Thank you for the reminder.

Because I had no idea of the original goal of TS, my goal was to try and attack it brutally (but wrong) to try solve the issue. If that works then details can then be addressed using a more sane approach. Initially I missed the bigger picture.

Today is tomorrow's yesterday.

wschulte

  • New Member
  • *
  • Posts: 39
Re: Secondary form does not refresh
« Reply #10 on: March 09, 2024, 05:47:46 am »
One word of warning here. Be very careful about scattering Application.ProcessMessages throughout the main thread of a program in an attempt to keep things running, and also putting Synchronize(shim) in a background thread so that e.g. a progress bar is updated.

If the shim calls something with an A.PM in it, you've got a potential recursion which can be somewhat difficult to track down if you're not alert to the possibility.

MarkMLl

Thanks for that. But what is an "A.PM" ?

wschulte

  • New Member
  • *
  • Posts: 39
Re: Secondary form does not refresh
« Reply #11 on: March 09, 2024, 05:52:41 am »
So .. tried to process the CSV file in a thread, but still the secondary form is updated only AFTER the thread is finished  :(

I changed the test program a bit, so it does what the real life case does (open a CSV document) and in a thread or not. It is in the attached zip file.

What keeps me bothering is the difference between doing all without opening a secondary form (messages are shown instantly) and with a secondary form (messages show only AFTER). I keep the feeling I am missing something here ...

BTW. in the real life case it is much worse: after opening the CSV document a database processing is taking place. This takes a lot of time, so the user is staring at the gray secondary form for a while.
« Last Edit: March 09, 2024, 05:56:05 am by wschulte »

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Secondary form does not refresh
« Reply #12 on: March 09, 2024, 08:46:35 am »
One word of warning here. Be very careful about scattering Application.ProcessMessages throughout the main thread of a program in an attempt to keep things running, and also putting Synchronize(shim) in a background thread so that e.g. a progress bar is updated.

If the shim calls something with an A.PM in it, you've got a potential recursion which can be somewhat difficult to track down if you're not alert to the possibility.

Thanks for that. But what is an "A.PM" ?

Look one para further up: Application.ProcessMessages. It's the call you've got to make occasionally, rather than having your main thread in a tight loop waiting for thread completion.

Oh, and para is a frequently-used abbreviation for "paragraph".

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018