[Lazarus] Thread finalization
Leonardo M. Ramé
l.rame at griensu.com
Wed Sep 21 14:31:27 CEST 2011
I'm having a rough time with Threads.
When my app starts, it creates an instance of a TThread that loads a
bunch of images. That TThread instance can be destroyed in two ways:
1) When the loading finishes.
2) When the user closes the app while loading.
In case 2, I destroy the thread using this code:
if Assigned(FMyThread) then
begin
FMyThread.Terminate;
while not FMyThread.Finished do
CheckSynchronize(100);
FreeAndNil(FMyThread);
end;
This works ok.
Now, in case 1, I thought that I should assign an OnTerminate event to
the thread, then destroy it, but it doesn't work as expected. The app
gets locked on my thread destroy event:
procedure TForm1.Createthread;
begin
FMyThread := TMyThread.Create;
FMyThread.OnTerminate := @ThreadDestroyEvent;
FMyThread.Start;
end;
procedure TForm1.ThreadDestroyEvent(Sender: TObject);
begin
FMyThread.Free;
end;
destructor TMyThread.destroy;
begin
// destroy internal objects
inherited destroy; // <-- The app gets locked here!!
end;
Why is this happening?, am I doing something wrong?.
--
Leonardo M. Ramé
http://leonardorame.blogspot.com
More information about the Lazarus
mailing list