Progress Bar hangs the application

1

I'm trying to increment progressbar within my repeat structure, but progressbar always hangs the application. I'll demonstrate here, follow the code:

Would it be correct to try to use this way?

ProgressBar1.Position:=0;
While ... do
begin

     DModuleGrid.ZQuery3.Close;
     DModuleGrid.ZQuery3.SQL.Clear;
     DModuleGrid.ZQuery3.SQL.Add('SELECT count(*) FROM cupatres WHERE numcupom = :pcoo2 ');
     DModuleGrid.ZQuery3.SQL.Add('AND NSerie = :pecf1 AND ccf = :pccf3 AND Retorno = :pret4 ');
     DModuleGrid.ZQuery3.SQL.Add('AND diavenda BETWEEN "'+pdate1treg+'" AND "'+pdate2treg+'"');
     DModuleGrid.ZQuery3.ParamByName('pcoo2').AsString := copy(lTemp,47,6);
     DModuleGrid.ZQuery3.ParamByName('pccf3').AsString := copy(lTemp,53,6);
     DModuleGrid.ZQuery3.ParamByName('pret4').AsString := copy(lTemp,59,3);
     DModuleGrid.ZQuery3.ParamByName('pecf1').AsString := copy(lTemp,4,20);
     DModuleGrid.ZQuery3.Open;
     DModuleGrid.ZQuery3.FetchAll;
     rc1 := DModuleGrid.ZQuery3.RecordCount-1;



ProgressBar1.Max := rc1;
ProgressBar1.Position:= ProgressBar1.Position+1;
   Sleep(50);
   Application.ProcessMessages;
end;
    
asked by anonymous 23.05.2014 / 18:49

2 answers

0

I've done a while only to count the number of records found, follow the code:

//contando registros
  AssignFile(txt, frmSelection.FileListBox1.FileName);
  Reset(txt);
  while not eof(txt) do
  begin
    Readln(txt, lTemp);
    btnLoadCup.Enabled := false;

    if (copy(lTemp, 1, 3) = 'E01') then
    begin
      date1 := StrToDateTime(copy(lTemp, 134,2)+'/'+copy(lTemp, 132,2)+'/'+copy(lTemp, 128,4));
      date2 := StrToDateTime(copy(lTemp, 142,2)+'/'+copy(lTemp, 140,2)+'/'+copy(lTemp,
      136,4));
      date1treg := FormatDateTime('yyyy/MM/dd', date1);
      date2treg := FormatDateTime('yyyy/MM/dd', date2);
    end;

    if  (copy(lTemp, 1, 3) = 'E14') then
    begin
       dtcompra:=StrToDateTime(copy(lTemp,65,2)+'/'+copy(lTemp,63,2)+'/'+copy(lTemp,59,4));
       dtcompratxt:= FormatDateTime('dd/MM/yyyy', dtcompra);

       DModuleGrid.ZQuery1.Close;
       DModuleGrid.ZQuery1.SQL.Clear;
       DModuleGrid.ZQuery1.SQL.Add('SELECT count(*) FROM tdcupant WHERE numcupom = :co2 AND ccf = :cc3 AND dtcompra = :dtc4 AND impcaixa = :ip5 ');
       DModuleGrid.ZQuery1.SQL.Add('AND dtcompra BETWEEN "'+date1treg+'" AND "'+date2treg+'"');
       DModuleGrid.ZQuery1.ParamByName('co2').AsString := copy(lTemp,53,6);
       DModuleGrid.ZQuery1.ParamByName('cc3').AsString := copy(lTemp,47,6);
       DModuleGrid.ZQuery1.ParamByName('ip5').AsString := copy(lTemp,4,20);
       DModuleGrid.ZQuery1.ParamByName('dtc4').AsDate := StrToDate(dtcompratxt);
       DModuleGrid.ZQuery1.Open;
       DModuleGrid.ZQuery1.FetchAll;
       rc1 := DModuleGrid.ZQuery1.RecordCount;


      if  (copy(lTemp, 1, 3) = 'E15') then
      begin
       DModuleGrid.ZQuery3.Close;
       DModuleGrid.ZQuery3.SQL.Clear;
       DModuleGrid.ZQuery3.SQL.Add('SELECT count(*) FROM cupatres WHERE numcupom = :pcoo2 ');
       DModuleGrid.ZQuery3.SQL.Add('AND NSerie = :pecf1 AND ccf = :pccf3 AND Retorno = :pret4 ');
       DModuleGrid.ZQuery3.SQL.Add('AND diavenda BETWEEN "'+pdate1treg+'" AND "'+pdate2treg+'"');
       DModuleGrid.ZQuery3.ParamByName('pcoo2').AsString := copy(lTemp,47,6);
       DModuleGrid.ZQuery3.ParamByName('pccf3').AsString := copy(lTemp,53,6);
       DModuleGrid.ZQuery3.ParamByName('pret4').AsString := copy(lTemp,59,3);
       DModuleGrid.ZQuery3.ParamByName('pecf1').AsString := copy(lTemp,4,20);
       DModuleGrid.ZQuery3.Open;
       DModuleGrid.ZQuery3.FetchAll;

        rc2 := DModuleGrid.ZQuery3.RecordCount;
        ProgressBar1.Max := rc1+rc2;
      end;
    end;
  end;
closeFile(txt);

Thanks, guys.

    
27.05.2014 / 12:53
4

Try as follows:

ProgressBar1.Position:=0;
While ... do
begin

  //codigo aqui

  ProgressBar1.Position:= ProgressBar1.Position+1;
  Sleep(500);
  Application.ProcessMessages;
end;

The command Application.ProcessMessages does what the name itself says, processes the messages that are pending on your system.

progressbar did not crash your application, it just did not progress because the system was "frozen" doing what your routine sent.

    
23.05.2014 / 19:13