ProgressBar not updating until end of loop?

Just starting out? Need help? Post your questions and find answers here.
Nudgy
User
User
Posts: 10
Joined: Mon May 27, 2024 8:11 pm

ProgressBar not updating until end of loop?

Post by Nudgy »

Hi all,

In a simple loop I am going through all the lines of a List and setting a ProgressBar accordingly. However, the progress bar does not actually change to 100% before the loop finishes entirely. I checked the documentation and forum, but didn't manage to find anything clearly relevant. My guess is that the loop is blocking the progress bar from updating (maybe some kind of threading or DoEvents equivalent is needed?).

For this project, I am using Linux - in case that makes a difference.

Any suggestions?

Thanks.
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: ProgressBar not updating until end of loop?

Post by jacdelad »

Is your event loop working while your progress stuff happens?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Axolotl
Enthusiast
Enthusiast
Posts: 798
Joined: Wed Dec 31, 2008 3:36 pm

Re: ProgressBar not updating until end of loop?

Post by Axolotl »

pretty sure, that jacdelad leads you to solve your problem.

Otherwise: Any code?
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Nudgy
User
User
Posts: 10
Joined: Mon May 27, 2024 8:11 pm

Re: ProgressBar not updating until end of loop?

Post by Nudgy »

jacdelad wrote: Fri Aug 09, 2024 4:21 pm Is your event loop working while your progress stuff happens?
I haven't touched the event loop for the purpose of the "List loop".

In simplified psuedo-code I am just doing more or less the following:

Code: Select all

Procedure x()

      SetGadgetAttribute(#ProgressBarID, #PB_ProgressBar_Minimum, 0)
      SetGadgetAttribute(#ProgressBarID, #PB_ProgressBar_Maximum, NumberOfTotalLinesInList)

     ForEach sourceList()

           SetGadgetState(#ProgressBarID, IncrementedNumberForEachLoop)

          ; Do some stuff with the List here (incuding adding lines to a ListIcon)
     Next

EndProcedure
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: ProgressBar not updating until end of loop?

Post by jacdelad »

When the function is called, the event loop waits until the function is done. No event processing -> no redrawing. Create a thread to process your stuff.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: ProgressBar not updating until end of loop?

Post by Fred »

And if you really don't want to a thread, you can put 'while windowevent() : wend' at regular interval to flush the events.
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: ProgressBar not updating until end of loop?

Post by jacdelad »

...but be aware that this way all events die painfully without being processed!
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Nudgy
User
User
Posts: 10
Joined: Mon May 27, 2024 8:11 pm

Re: ProgressBar not updating until end of loop?

Post by Nudgy »

Fred wrote: Fri Aug 09, 2024 5:19 pm And if you really don't want to a thread, you can put 'while windowevent() : wend' at regular interval to flush the events.
Thanks, Fred. This was actually the first thing I tried, with inspiration from the forum. But it returned the error "[ERROR] WindowEvent(): WindowEvent() and WaitWindowEvent() can not be called from a 'binded' event callback." Unfortunately, I am not smart enough to figure out what that error means :D I guess it might be caused by the use of BindEvents elsewhere in the code somehow. If you have any hints in that regard, it would be much appreciated.

Otherwise, this might be a good opportunity for me to learn about threading ;)
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: ProgressBar not updating until end of loop?

Post by jacdelad »

It means exactly what it says: You used Bind...() to let the procedure be automatically called. Procedures called by that are not allowed to use WaitWindowEvent(), because this could cause a fatal sprial of events calling the procedure over and over, stacking it into infinty.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: ProgressBar not updating until end of loop?

Post by DarkDragon »

jacdelad wrote: Fri Aug 09, 2024 5:38 pm ...but be aware that this way all events die painfully without being processed!
And you can slow down the process by sending events by e.g. moving your mouse.
bye,
Daniel
Post Reply