PDA

Ver la Versión Completa : como crear hilos de ejecucion en delphi 2007


pablopessoa
23-10-2008, 18:17:36
hola gente, estoy usando Code Gear delphi for win32 y quiero hacer un hilo de ejecucion para un proceso. Busque en internet y encontre esto :


//Para ello lo que hacemos es crear un hilo de ejecución heredando de la clase TThread del siguiente modo:

THilo = class( TThread )
Ejecutar: procedure of object;
procedure Execute; override;
end;

//La definición anterior hay que colocarla dentro del apartado Type de nuestra unidad


pero cuando agrego la unidad me dice que falta el archivo TThread.dcu.

me pongo a mirar las componentes de delphi y hay una que dice Thread Object, pero no se como se usa, alguien me puede ayudar?

muchas gracias!!!

elcolo83
23-10-2008, 19:11:08
Lo que tienes que hacer es crear la unidad del hilo (por una cuestion de proligidad, je) y luego incluirla en el uses de la unidad en la cual usaras ese hilo.
Aca te dejo una unidad que yo uso para hacer descargas desde la web para que veas un ejemplo.


unit ThDescarga;

interface

uses
Classes, IdHTTP, Messages, sysutils, IdComponent, shellApi;

type
TDescarga = class(TThread)
procedure SetParam(_WebFile, _Destino: String);//; _ObjProg: TObject);
Procedure UpdateProg;
private
{ Private declarations }
protected
procedure Execute; override;
public
Progreso: integer;
//ObjProg: TObject;
WebFile: String;
Destino: String
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 Descarga.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }

{ Descarga }
uses Prin, dialogs, FuncionesVarias;

Procedure TDescarga.UpdateProg;
begin
//Principal.ProgresoDescarga.Position:=
end;

procedure TDescarga.SetParam(_WebFile, _Destino: String);//; _ObjProg: TObject);
begin
WebFile:= _WebFile;
Destino:= _Destino;
// ObjProg:= _ObjProg;
end;


procedure TDescarga.Execute;
var ArchDestino: TFileStream;
BorrarArch: Boolean;
i: integer;
s, Pass: String;
const a=33;
begin
{ Place thread code here }
{ with TidHttp.Create(self)do
try

finally
Free;
end;}
//OnTerminate:= Principal.SubMenu.OnClick;
FreeOnTerminate:=True;
if pos('/PROGRAM/', Principal._Dst)>0 then
begin
Delete(Principal._Dst, 1, 9);
Principal._Dst:= PChar(ExtractFilePath(ParamStr(0)))+Principal._Dst;
end;
if (Principal.Descargando)and((Not FileExists(Principal._Dst))or
(lowercase(ExtractFileExt(Principal._Dst))='.up')) then
begin
if Not DirectoryExists(Principal._Dst) then
ForceDirectories(ExtractFilePath(Principal._Dst));
ArchDestino:=TFileStream.Create(Principal._Dst, fmCreate);
try
BorrarArch:= False;
try
Principal.IdHTTP.Get(Principal._WF, ArchDestino);
except
BorrarArch:= True;
end;
finally
if ArchDestino.Size <> Principal.FTotalBytes then
BorrarArch:= True;
ArchDestino.free;
Principal.Descargando:= False;
Principal.ProgresoDescarga.Visible:= False;

if BorrarArch then
DeleteFile(Principal._Dst)
else
begin
if lowercase(ExtractFileExt(Principal._Dst))='.up' then
begin
Principal.ListaDescarga.Lines.Clear;
Principal.ListaDescarga.Lines.LoadFromFile(Principal._Dst);
Pass:= '#File,Get'+Chr(a+100);
for i:= 0 to Principal.ListaDescarga.Lines.Count - 1 do
begin
s:= Principal.ListaDescarga.Lines[i];
Principal.ListaDescarga.Lines[i]:= DesCifrar(S, Pass);
if Pass='#File,Get'+Chr(a+100) then
Pass:= '#File,Dest'+Chr(a+100)
else Pass:='#File,Get'+Chr(a+100);
end;
DeleteFile(Principal._Dst);
Principal.IdHTTPWorkEnd(Self, wmRead);
end
else if lowercase(ExtractFileExt(Principal._Dst))='.exe' then
begin
try
if (lowercase(ExtractFileName(Principal._Dst))<>'update.exe')then
ShellExecute(0, '', PChar(Principal._Dst), PChar('/'+PChar(ParamStr(0))), '', 0);
except
Principal.Log('Error al Ejecutar: '+Principal._Dst);
end;
{if (lowercase(ExtractFileName(Principal._Dst))='update.exe')then
Principal.AutoClose.Enabled:= True;}
end;
end;
end;
end
else
Principal.Descargando:= False;
Principal.ProgresoDescarga.Visible:= False;
end;
end.