Using Threads to Update Windows GUI

Asked

Viewed 204 times

2

I have a thread I want you to update a Progress bar to every iteration of a TQuery for example, but what I noticed is that to carry out the query process, I have to put the function that makes select inside the 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 bar? That is to take select from this thread and leave it only to update the form’s GUI?

1 answer

1

I use it as follows:

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 in Button Click for example, you will scroll through the Query and update the progress without using the thread

  • Application.ProcessMessages wouldn’t be a bad thing in an iteration, it will process any mail pending from windows

  • If you need your View to have some visual effect with the return of a controller data, you’ll have to work with messages/notifications.

  • Take a look at the omnithreadlibrary. They have an easy and practical way to implement what you need. https://github.com/gabr42/OmniThreadLibrary

  • @Victortadashi So, I’m doing this in Delphi 7, I don’t think so

Browser other questions tagged

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