Recent

Author Topic: Updating ProgressBar from threads  (Read 8650 times)

kapibara

  • Hero Member
  • *****
  • Posts: 610
Updating ProgressBar from threads
« on: July 20, 2014, 07:25:03 pm »
I'm updating a ProgressBar from a thread. The problem is that the movement of the progressbar is delayed and never finishes. When I use a TrackBar instead, there is no delay and it runs to the end.

Why is that? If you check the demo you'll see what I mean.

(As you can see I have added OnDestroy and OnProgress Events to the thread).

Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Updating ProgressBar from threads
« Reply #1 on: July 20, 2014, 09:30:45 pm »
On Windows (win32, Win7) your demo performs OK; progressbar matches trackbar perfectly.
What widgetset gives you problems?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Updating ProgressBar from threads
« Reply #2 on: July 20, 2014, 10:48:17 pm »
No problem on x86_64-linux-qt, your code runs well.

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: Updating ProgressBar from threads
« Reply #3 on: July 21, 2014, 05:34:11 am »
This happens under 64 bit Windows (Win7/Win8). Tested under Linux Ubuntu 64 now, and that works fine.

Under Win64 the ProgressBar never gets to the end before its Position is reset to 0 by the OnDestroyed event handler. Tried with Laz release and Laz 64 trunk with fpc 64bit 2.6.4 and 2.7.1.

Speed of movement is the same so I suspect the delay before the ProgressBar starts to move. Somehow it moves immediately under Linux (and win32?) but not on win64.

However, if I workaround and reset the ProgressBar.Position to 0 in the buttons Click-event instead, then it gets to the end. But it starts much later than the trackbar so it also reaches the end much later.
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Updating ProgressBar from threads
« Reply #4 on: March 11, 2015, 09:13:56 pm »
What is the reason for this behaviour? I ran into the same problem. The ProgressBar shows 100% about 0,5 sec after setting Position to max.
How to fix this?
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Updating ProgressBar from threads
« Reply #5 on: March 11, 2015, 11:37:06 pm »
Maybe Windows itself.
There progressbar has a nasty habbit of not directly showing Position, but it "grows" to it.
Test with themes disabled (shows "old-time" progressbar), if it behaves correctly then, you identified the problem.

You bypass this behaviour by decreasing Position, Windows will draw the progressbar's position immediately.

Code: [Select]
  ProgressBar.Position := WantedPosition + 1;
  ProgressBar.Repaint;  //IIRC this is needed, or Application.ProcessMessage
  ProgressBar.Position := WantedPosition;

Bart

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: Updating ProgressBar from threads
« Reply #6 on: March 12, 2015, 04:18:10 am »
There are also a lot of progress bars with source code over at Torrys. Takes just a minute to convert with Lazarus->Tools->Delphi conversion. Attached picture of some I found useful.

http://www.torry.net/pages.php?id=504

Edit: uploaded a demo containing progressbar sources. (Debug info is de-activated in project settings, and symbols stripped).
« Last Edit: March 12, 2015, 04:41:44 am by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Updating ProgressBar from threads
« Reply #7 on: March 12, 2015, 12:20:18 pm »
Okay,

thanks for the tips. I have a workaround now.
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Updating ProgressBar from threads
« Reply #8 on: July 08, 2018, 02:55:37 pm »
Yes, in Windows 7 this workaround updates the progress bar correctly:
Code: Pascal  [Select][+][-]
  1. ProgressBar.Position := ProgressBar.Position + 2;
  2. ProgressBar.Repaint;
  3. ProgressBar.Position := ProgressBar.Position - 1;
keep it simple

 

TinyPortal © 2005-2018