unit uthrdCampanha; interface uses System.Classes, ZDataset, System.SysUtils, Vcl.Forms, System.UITypes; type TQueryCampanhaInThread = class(TThread) private // sql_text: Tstrings; _Form: TForm; { Private declarations } protected procedure Execute; override; procedure AttJanela; public constructor Create(CreateSuspended: Boolean; AForm: TForm); end; implementation { Important: Methods and properties of objects in visual components can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TQueryCampanhaInThread.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; or Synchronize( procedure begin Form1.Caption := 'Updated in thread via an anonymous method' end ) ); where an anonymous method is passed. Similarly, the developer can call the Queue method with similar parameters as above, instead passing another TThread class as the first parameter, putting the calling thread in a queue with the other thread. } uses ucadCampanha; { TQueryCampanhaInThread } procedure TQueryCampanhaInThread.AttJanela; var MForm: TcadCampanha; begin MForm := _Form as TcadCampanha; with MForm do begin dbgrd_camp.Columns[0].Title.Caption := 'ID'; dbgrd_camp.Columns[1].Title.Caption := 'Nome'; dbgrd_camp.Columns[2].Title.Caption := 'Documento'; dbgrd_camp.Columns[3].Visible := false; dbgrd_camp.Columns[4].Visible := false; queryCampanha.First; queryCampanha.DisableControls; total_titulos := 0; valor_total := 0; while not queryCampanha.Eof do begin total_titulos := total_titulos + queryCampanha.FieldByName('quant') .AsInteger; valor_total := valor_total + queryCampanha.FieldByName('total').AsFloat; queryCampanha.Next; end; queryCampanha.First; queryCampanha.EnableControls; lbl_qntdevedores.Caption := 'Devedores: ' + IntToStr(queryCampanha.RecordCount) + '.'; lbl_qnttitulos.Caption := 'Títulos: ' + IntToStr(total_titulos) + '.'; lbl_valorestitulos.Caption := 'Total: R$' + FormatFloat('0.00', valor_total) + '.'; // FormatFloat('0.00',valor_total) Screen.Cursor := crDefault; if queryCampanha.RecordCount > 0 then begin btn_ok.Enabled := true; // pb_campanha.max := queryCampanha.RecordCount; queryCampanha.First; end else begin btn_ok.Enabled := false; end; pnl_txt.Caption := 'Pesquisa realizada.'; cadCampanha.pesquisando := false; end; end; constructor TQueryCampanhaInThread.Create(CreateSuspended: Boolean; AForm: TForm); begin inherited Create(CreateSuspended); _Form := AForm; Self.FreeOnTerminate := true; end; procedure TQueryCampanhaInThread.Execute; begin { Place thread code here } while not Terminated and (cadCampanha.pesquisando) do begin with cadCampanha do begin queryCampanha.Open; // dbgrd_camp.Columns[0].Title.Caption := 'ID'; // dbgrd_camp.Columns[1].Title.Caption := 'Nome'; // dbgrd_camp.Columns[2].Title.Caption := 'Documento'; // dbgrd_camp.Columns[3].Visible := false; // dbgrd_camp.Columns[4].Visible := false; // // queryCampanha.First; // queryCampanha.DisableControls; // total_titulos := 0; // valor_total := 0; // while not queryCampanha.Eof do // begin // total_titulos := total_titulos + queryCampanha.FieldByName('quant') // .AsInteger; // valor_total := valor_total + queryCampanha.FieldByName('total').AsFloat; // queryCampanha.Next; // end; // queryCampanha.First; // queryCampanha.EnableControls; // // lbl_qntdevedores.Caption := 'Devedores: ' + // IntToStr(queryCampanha.RecordCount) + '.'; // lbl_qnttitulos.Caption := 'Títulos: ' + IntToStr(total_titulos) + '.'; // lbl_valorestitulos.Caption := 'Total: R$' + FormatFloat('0.00', // valor_total) + '.'; // // FormatFloat('0.00',valor_total) // Screen.Cursor := crDefault; // // if queryCampanha.RecordCount > 0 then // begin // btn_ok.Enabled := true; // // pb_campanha.max := queryCampanha.RecordCount; // queryCampanha.First; // end // else // begin // btn_ok.Enabled := false; // end; // pnl_txt.Caption := 'Pesquisa realizada.'; end; Self.Synchronize(AttJanela); Self.Terminate; end; cadCampanha.pesquisando := false; end; end.