vendredi 22 mai 2015

Delphi - Custom thread not being created

i'm having a bit of a problem here. I have a custom class that inherits TPersistent class, inside(private section) this custom class, i have a custom made TThread with overriden Execute method which fires every (1000 ms). Everything works great, until i move my 2 custom classes to a new Unit...

type
  TMyThread= class(TThread)
  protected
   procedure Execute; override;
  end;

  TMyClass = class(TPersistent)
  private
   T: TMyThread;
  protected
   constructor Create;
  public
   destructor Destroy; override;
  end;


implementation

procedure TMyThread.Execute;
begin
 while not Self.Terminated do begin
  Sleep(1000);
  MessageBox(0, 'test', nil, MB_OK)
 end;
end;

constructor TMyClass.Create;
begin
 inherited Create;
 t := Ttt.Create(False);
end;

destructor TMyClass.Destroy;
begin
 t.Terminate;
 t.WaitFor;
 FreeAndNil(t);
 inherited Destroy;
end;

The above code works great in the main project unit, but when i move it to a new unit, the thread code no longer works, i get an AV, when i try to free a TMyClass object. I think the thread is not being constructed at all, and that's why i get an AV when i try to free it... but why? it shouldn't matter in which Unit the code is...

Aucun commentaire:

Enregistrer un commentaire