Recent

Author Topic: Progress bar problem  (Read 19312 times)

Ruptor

  • Full Member
  • ***
  • Posts: 191
Progress bar problem
« on: January 30, 2013, 01:56:09 pm »
The progress bar won't work for me or it might be I am using it in the wrong fashion.  The maximum limit doesn't appear to be working. I open a big file and read through each line expecting the progress bar to follow from 0 to Str.count=1539318 but the bar fills in 20 seconds and the code doesn't exit for another minute. Have I done something stupid in the code?
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
var
  Str: TStringList;
  PriceData: String;
  i:Longint;
  opened:boolean;
begin
  opened:=true;
  Str := TStringList.Create;
  try
    Str.LoadFromFile(FileListBox1.FileName);
  except
    opened:=false;
    Edit2.text:='Cannot open file';
  end;
  if opened then
  begin
    if FileListBox1.Items.Count-1<>-1 then
    begin
      ProgressBar1.Min:=0;
      ProgressBar1.Max:=Str.Count;
      ProgressBar1.Step:=1;                 /////////////////////////////////////////// mod
      ProgressBar1.StepBy(1);
      Edit2.text:=inttostr(Str.Count);
      i:=0;
      while i<Str.Count do
      begin
        PriceData:=Str[i];
        Edit2.text:=PriceData;
        Application.ProcessMessages;
        ProgressBar1.StepIt;
        i:=i+1;
      end;
      Edit2.text:=inttostr(i);
    end else
    begin
      Edit2.text:='File Empty';
    end;
  end;
  Str.SaveToFile(FileListBox1.FileName);
  Str.Free;
end;
« Last Edit: January 31, 2013, 03:14:07 pm by Ruptor »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8799
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Progress bar problem
« Reply #1 on: January 30, 2013, 04:37:58 pm »
Quote
ProgressBar1.StepIt;
Application.ProcessMessages;
i:=i+1;

Ruptor

  • Full Member
  • ***
  • Posts: 191
Re: Progress bar problem
« Reply #2 on: January 31, 2013, 12:04:40 am »
Thanks for the suggestion. The "Application.ProcessMessages;" has made the display more real but it hasn't solved the progress bar problem. The bar now fills in a split second but the edit box continues to scroll for another minute. Since the edit box is now running in real time the progress bar should also tick up once for every edit box write but it don't.

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Progress bar problem
« Reply #3 on: January 31, 2013, 12:08:30 am »
what is str.count related too ?

TstringList,  a control  its not defined as a var so this is why  I ask

I can't replicate your issue because there is to little infomation

Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Progress bar problem
« Reply #4 on: January 31, 2013, 12:40:47 am »
This works for me  durring actual run change increment from 1000 to 1

Procedure TForm1.Button1Click(Sender: TObject);
Begin
  progressbar1.Max:= 1539318  ;
 progressbar1.position := progressbar1.Position + 1000;
end;   

also  set progressbar1.position := 0;   in form on activate

Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

Ruptor

  • Full Member
  • ***
  • Posts: 191
Re: Progress bar problem
« Reply #5 on: January 31, 2013, 12:55:23 am »
Sorry I was trying to simplify the problem but I have modified my original post with the whole procedure now if that helps. When a big file of 0.5 Gbytes is used the progress bar finishes but the edit box continues to scan through the rest of the file.

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Progress bar problem
« Reply #6 on: January 31, 2013, 01:34:21 am »
Tell you what zip up your project directory
and post it on a file host web site and give me the link


A good filehost is dropbox

https://www.dropbox.com/home
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

Ruptor

  • Full Member
  • ***
  • Posts: 191
Re: Progress bar problem
« Reply #7 on: January 31, 2013, 01:45:52 am »
On your test with +1000 did your progress bar move 1/1500th of the length of the bar?

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Progress bar problem
« Reply #8 on: January 31, 2013, 01:52:37 am »
Yes because Progressbar1.Max := 1539318
1.5 million postions would take forever to increment with my example
so I used a 1000 per increment to speed up the test.

If you zip up your project directory and upload to a file server I can
see whats going on for you.


but im not going to rewrite your entire program to find out whats going on
it would be to time consumming for me at this time. But im willing to find out
whats going on if you upload your project.

« Last Edit: January 31, 2013, 01:55:36 am by wjackson153 »
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

Ruptor

  • Full Member
  • ***
  • Posts: 191
Re: Progress bar problem
« Reply #9 on: January 31, 2013, 02:13:45 am »
Here is my project. The dumpmid.csv is much shorter but you can still see the progress bar finishes long before the file has been read. You need to change the directory to where you put the csv file then you can just click it and hit run.

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Progress bar problem
« Reply #10 on: January 31, 2013, 02:29:16 am »
below is your fixed code.

Code: [Select]


Unit Unit1;

{$mode objfpc}{$H+}

Interface

Uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  StdCtrls, FileCtrl;

Type

  { TForm1 }

  TForm1 = Class(TForm)
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    FileListBox1: TFileListBox;
    ProgressBar1: TProgressBar;
    Procedure Button2Click(Sender: TObject);
  Private
    { private declarations }
  Public
    { public declarations }
  End;

Var
  Form1: TForm1;

Implementation

{$R *.lfm}

{ TForm1 }



procedure TForm1.Button2Click(Sender: TObject);
var
  Str: TStringList;
  PriceData: String;
  i:Longint;
  opened:boolean;
begin
  opened:=true;
  Str := TStringList.Create;
  try
    Str.LoadFromFile(FileListBox1.FileName);
  //Str.LoadFromFile(UTF8ToSys(FileListBox1.FileName));
  except
    opened:=false;
    Edit2.text:='Cannot open file';
  end;
  if opened then
  begin
    if FileListBox1.Items.Count -1 <> -1 then
    begin
      ProgressBar1.Min:=0;
      ProgressBar1.Max:=Str.Count;
//      ProgressBar1.Max:=FileListBox1.Items.Count;
      ProgressBar1.Position:= progressbar1.Position + 1;

      i:=0;
      while i<Str.Count do
      begin
         Application.ProcessMessages;
        PriceData:=Str[i];
        Edit2.text:=PriceData;

       progressbar1.Position:= progressbar1.Position + 1;
        i:=i+1;
      end;
      Edit2.text:=inttostr(i);
    end else
    begin
      Edit2.text:='File Empty';

    end;

  end;
   Edit2.text:=inttostr(Str.Count);
  Str.SaveToFile(FileListBox1.FileName);
  Str.Free;
end;


End.

Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

Ruptor

  • Full Member
  • ***
  • Posts: 191
Re: Progress bar problem
« Reply #11 on: January 31, 2013, 02:50:13 am »
Thanks. What is the point in having "stepit" etc if the only way to make it work is do it manually or have I misunderstood the usage of "stepit"?

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Progress bar problem
« Reply #12 on: January 31, 2013, 02:58:57 am »
Advances the current position for a progress bar control by the step increment
and redraws the bar to reflect the new position.


but application.processmessages; in our loop so we can avoid using stepit


Has this solution solved your problem?

Seems to be working fine on my end

Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

Ruptor

  • Full Member
  • ***
  • Posts: 191
Re: Progress bar problem
« Reply #13 on: January 31, 2013, 03:08:27 am »
yes it works now but it sounds like you are saying stepit should work the same but doesn't.

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Progress bar problem
« Reply #14 on: January 31, 2013, 03:12:09 am »
As i said before mate I never had any luck using stepit

I rather use .position that has always worked flawless for me.
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

 

TinyPortal © 2005-2018