Progress Bar locks application

Asked

Viewed 1,043 times

1

I’m trying to raise a progressbar within my repeating structure, however the progressbar always locks the application. I will demonstrate here, follows the code:


It would 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;
  • What is the "code here"? Sure it is the progressbar? It works without it?

  • @Filipe.Fonseca, works smoothly without it (progressbar), the code is huge.

  • Does the huge code access bank? You can tell if the system freezes while running the while without having the progressbar there?

  • Try to add application.processmessages; in place of Sleep. If I get a response explaining the pq.

  • @Filipe.Fonseca, the code accesses bank yes, I’m testing with the application.processmessages;, if it works I return! Thanks!

  • @Fonseca, it worked, yes, he passed the code right, however, the progressibar does not process, it stands at 100%.

  • I would discourage you from using Sleep unless you have a clear reason for it. It causes the processor to swap threads and does not help in anything to the progress of the method and taking another half second at each step of the loop for the user

  • @Caputo, what would encourage then friend? Wanted something to make the bar Progress run without crashing ;/

  • 2

    We are with a big problem here right now. But in the late afternoon put an example of how to do. A question: What is the maxvalue of your bar Progress? how are you calculating it? In your example does not show the maxvalue setting

  • @Caputo, this is the problem, because when I put the maxvalue of the progressibar, which in this case would be the record of the query, but, does not return me anything, I believe q the amount of records is picking from another corner.

  • @DBX8, it’s actually pq. he’s just logging into the database to grab and compare the records I need, he’s not going through the database completely.

Show 6 more comments

2 answers

4

Try it this way:

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 says, processes the messages that are pending in your system.

The progressbar did not lock its application, only did not advance because the system was "frozen" doing what your routine ordered.

0


I did a while just to count the amount 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, thank you guys.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.