Using Threads to update windows GUI

2

I have thread that I want to update a progress bar at each iteration of a TQuery for example, however what I noticed is that to perform the query process, I have to put the function that makes select within thread, as an example below:

procedure TGeradorArquivos.GeraArquivo;
var
   CaminhoArquivo: String;
   Arquivo: TextFile;
begin
   Self.Query.Sql.Clear;
   Self.Query.Sql.Add('Select * from Teste');
   Self.Query.open;
   if Self.Query.Recordcount > 0 then
   begin
      AssignFile(Arquivo,CaminhoArquivo);
      Rewrite(Arquivo);
      FrmTeste.ProgressBar.Max := Self.Query.RecordCount;
      try
         while not Self.Query.Eof do
         begin
            Write(Arquivo,Self.Query.FieldByName('Campo').AsString);                       
            FrmTeste.ProgressBar.StebBy(1);
            Self.Query.Next;
         end;
      finally
         CloseFile(Arquivo);
      end;
   end;
end;

Is there any way to unlink the function to update the progress bar? Ie take the select from this thread and just leave to update the GUI of the form?

    
asked by anonymous 31.03.2017 / 14:43

1 answer

1

I use the following form:

while (Query.Eof = False) do
begin
  write(Arquivo,Query.FieldByName('Campo').AsString);                       
  FrmTeste.ProgressBar.StebBy(1);
  Application.ProcessMessages; {Atualiza tudo no Formulário}
  Query.Next;
end;

If you run this block under Button Click, for example, you have traversed Query and will update the progress without using thread

    
31.03.2017 / 17:30