PDA

Ver la Versión Completa : Problema con hilos


zajoma
13-04-2009, 08:40:08
Hola a todos.

Estoy intentando, con Delphi 7, Crear una aplicación con hilos pero cuando lo ejecuto me sale un error que dice:

Project Project1.exe raised exception class EThread with message 'Thread Error: Controlador no válido (6)'. Process stopped. Use Step or Run to continue.

A qué puede ser debido. Un saludo.

dec
13-04-2009, 09:55:13
Hola,

El mensaje de error está muy bien puesto, pero, me temo que sea preciso algo de código para encontrar el posible problema. Si pudieras concretar el error en unas cuantas líneas de código y pudieras copiarlas aquí... igual alguien estaría en disposición de echarte una mano. ;)

zajoma
13-04-2009, 12:06:29
He creado un hilo elemental de la forma

uses ... , SyncObjs, ...;

type
TMiHilo = class( TThread )
private
FinternalData:TStringList;
FEvent:TEvent;
public
constructor Create( CreateSuspended:boolean );
function LeerCadena( timeout:integer ):string;
procedure Execute(); override;

property Event:TEvent read FEvent;
end;

constructor TMiHilo.Create( CreateSuspended:boolean );
begin
FInternalData := TStringList.Create();
FEvent := TEvent.Create( nil, false, false, '' );
end;

function TMiHilo.LeerCadena( timeout:integer ):string;
var
res:TWaitResult;
begin
if ( timeout = 0 ) then res := FEvent.WaitFor( INFINITE )
else res := FEvent.WaitFor( timeout );

if ( res = wrSignaled ) then begin
result := FInternalData[0];
FInternalData.Delete(0);
if FInternalData.Count > 0 then FEvent.SetEvent;
end else
if ( res = wrTimeout ) then result := 'timeout' else result := 'error';
end;

procedure TMiHilo.Execute();
begin
while not Self.Terminated do begin
ReadDataFromFile();
FEvent.SetEvent;
Sleep(1000);
end;
end;



Un saludo y gracias por el interés.

white_zombie
13-04-2009, 13:17:17
Hola, el problema lo tienes en el constructor create, añade esta linea

constructor TMiHilo.Create( CreateSuspended:boolean );
begin
FInternalData := TStringList.Create();
FEvent := TEvent.Create( nil, false, false, '' );
inherited create(CreateSuspended);
end;

Un Saludo.

zajoma
13-04-2009, 16:48:15
Hola white_zombie. Te agradezco la gran ayuda que me has brindado. Me has solucionado el problema. Muchas Gracias.
Un saludo.