Recent

Author Topic: How to start/stop marquee ProgressBar?  (Read 5829 times)

Vodnik

  • Full Member
  • ***
  • Posts: 210
How to start/stop marquee ProgressBar?
« on: March 24, 2018, 12:00:26 pm »
Hello, I cant find answer for this trivial question  :(
I want ProgressBar to indicate process while my application is querying the database.
I suppose it is not predictable how long process will take, so I use marquee ProgressBar
But can't find how to start it before the query and stop after.
Being just dropped to the form, ProgressBar is running all the time, hanging for the period while DB query is actually performed.
Playing with ProgressBar.Show and ProgressBar.Hide had no success: it is not running when shown.

wp

  • Hero Member
  • *****
  • Posts: 11833
Re: How to start/stop marquee ProgressBar?
« Reply #1 on: March 24, 2018, 12:04:57 pm »
Show the progressbar only when the process is running?

Or: Start with Style = pbstNormal. When the process starts switch it to Style = pbstMarquee --> Marquee is running. When the process is complete switch it back to pbstNormal --> Marquee disappears.

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: How to start/stop marquee ProgressBar?
« Reply #2 on: March 24, 2018, 12:30:09 pm »
Well, this doesn't work, too.
Seems that database query process completely locks the application, so ProgressBar doesn't run.
I have a bad feeling that there is no simple solution and I have to go into threads, etc.  :(

totya

  • Hero Member
  • *****
  • Posts: 720
Re: How to start/stop marquee ProgressBar?
« Reply #3 on: March 24, 2018, 02:00:57 pm »
Well, this doesn't work, too.
Seems that database query process completely locks the application, so ProgressBar doesn't run.
I have a bad feeling that there is no simple solution and I have to go into threads, etc.  :(

If one simple application is running, this is use one thread. This is mean, UI is freeze (not refreshed) while the main process is busy. UI refresh from the second thread is not a good idea, because of the main UI refresh need Synchronize method, but Synchronize method not executed, while the main process is busy... :)

Solution (without to use additional query thread): If this database query isn't processed only with the database engine, insert Application.ProcessMessages to the the your database query code.

See this Application.ProcessMessages sample. Button1 and 2 execute with same code, but while Button2 process is running, UI is refreshed frequently.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  9.   ComCtrls, StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     ProgressBar1: TProgressBar;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure Button2Click(Sender: TObject);
  21.   private
  22.     { private declarations }
  23.   public
  24.     { public declarations }
  25.   end;
  26.  
  27. const
  28.   cWaitTime = 800000000;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.Button1Click(Sender: TObject);
  40.  var i,j: int64;
  41. begin
  42.   for i:= 0 to cWaitTime do j:=i;
  43.  
  44.   ShowMessage('OK');
  45. end;
  46.  
  47. procedure TForm1.Button2Click(Sender: TObject);
  48.  var i,j: int64;
  49. begin
  50.   for i:= 0 to cWaitTime do
  51.   begin
  52.     j:=i;
  53.  
  54.     if i mod (cWaitTime div 50) = 0 then Application.ProcessMessages;
  55.   end;
  56.  
  57.   ShowMessage('OK');
  58. end;
  59.  
  60. end.
  61.  
« Last Edit: March 24, 2018, 02:26:34 pm by totya »

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: How to start/stop marquee ProgressBar?
« Reply #4 on: March 24, 2018, 02:32:39 pm »
totya, thank you for explanation and for example

Database query is standard Laz/FPC procedure:
Code: Pascal  [Select][+][-]
  1.    
  2.     ProgressBar.Style:=pbstMarquee;
  3.     DBForm.DBquery.SQL.text:='select * from callrecord' ;
  4.     DBForm.DBquery.Open;
  5.     ProgressBar.Style:=pbstNormal;
  6.  
Seems that Application.ProcessMessages will not help in my case.
The query is processed by external database engine, so Lazarus have no control over it, I suppose.
The only alternative I see is to show static message "Please wait...", that doesn't look very nice for the user. He have to see something is changing to believe that process have not hang.
« Last Edit: March 24, 2018, 02:35:49 pm by Vodnik »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to start/stop marquee ProgressBar?
« Reply #5 on: March 24, 2018, 03:13:04 pm »
totya, thank you for explanation and for example

Database query is standard Laz/FPC procedure:
Code: Pascal  [Select][+][-]
  1.    
  2.     ProgressBar.Style:=pbstMarquee;
  3.     DBForm.DBquery.SQL.text:='select * from callrecord' ;
  4.     DBForm.DBquery.Open;
  5.     ProgressBar.Style:=pbstNormal;
  6.  
Seems that Application.ProcessMessages will not help in my case.
The query is processed by external database engine, so Lazarus have no control over it, I suppose.
The only alternative I see is to show static message "Please wait...", that doesn't look very nice for the user. He have to see something is changing to believe that process have not hang.
Code: Pascal  [Select][+][-]
  1.  Screen.Cursor := crSQLWait;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: How to start/stop marquee ProgressBar?
« Reply #6 on: March 24, 2018, 06:04:32 pm »
Thank's for an idea, taazz!

 

TinyPortal © 2005-2018