Ver Mensaje Individual
  #1  
Antiguo 10-05-2007
Avatar de pborges36
pborges36 pborges36 is offline
Miembro
 
Registrado: oct 2004
Ubicación: Argentina
Posts: 192
Reputación: 20
pborges36 Va por buen camino
Question ejecutar consulta sql en hilo

Holas, soy nuevo en esto de los hilo. La idea es la sig: he visto en algunos sistemas que mientras se cargan los datos en una grilla ya se puede interactuar con la misma grilla. Utilizo delphi 6 y mysql, la consulta la pongo en un TQuery. Creo que esto se podria hacer con un hilo, segun lei. Hice un intento de crear un hilo el cual lo unico que hace es activar el TQuery. Despues de hacer varios intentos el codigo quedo mas o menos asi:

Código Delphi [-]
unit UnitHilos;
interface
uses
  Classes, DBTables, DBLocalB;
type
  TQuerythread = class(TThread)
  private
    Q:TQuery;
    { Private declarations }
  protected
    procedure Execute; override;
    procedure Activar;
  public
    constructor Create(Q1: TQuery; ThreadPriority: TThreadPriority);
  end;

type
  TClientDatasetthread = class(TThread)
  private
    QClient:TbdeClientDataset;
    { Private declarations }
  protected
    procedure Execute; override;
    procedure Activar;
  public
    constructor Create(Q1: TbdeClientDataset; ThreadPriority: TThreadPriority);
  end;
 
 
implementation
{ Important: Methods and properties of objects in VCL or CLX can only be used
  in a method called using Synchronize, for example,
      Synchronize(UpdateCaption);
  and UpdateCaption could look like,
    procedure TQuerythread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }
{ TQuerythread }
constructor TQuerythread.Create(Q1: TQuery; ThreadPriority: TThreadPriority);
begin
    inherited Create(True);
    Q:=Q1;
end;
procedure TQuerythread.Activar;
begin
//    q.Active:=False;
    if not q.Active then
      q.Active:=True;
end;
procedure TQuerythread.Execute;
begin
  { Place thread code here }
    while True do
        Synchronize(Activar);
end;

constructor TClientDatasetthread.Create(Q1: TbdeClientDataset; ThreadPriority: TThreadPriority);
begin
    inherited Create(True);
    QClient:=Q1;
end;
procedure TClientDatasetthread.Activar;
begin
    if not QClient.Active then
      QClient.Active:=True;
end;
procedure TClientDatasetthread.Execute;
begin
  { Place thread code here }
    while True do
        Synchronize(Activar);
end;

end.

En el form hago lo sig:


Código Delphi [-]
var
  QTh:TClientDatasetthread;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
     query1.CommandText:='select * from prodcutos';
     QTh:=TClientDatasetthread.Create(Query1,tpLowest);
end;
 
procedure TForm1.MostrarClick(Sender: TObject);
begin
      query1.Active:=false;
      QTH.Resume;
end;

Aun asi la ventana no responde mientras se activa el Tquery y para el segundo click en el boton el TQuery ya no se activa. Espero puedan ayudarme. Saludos.
Responder Con Cita