Help for TThread.OnTerminate Event
Occurs after the thread's Execute method has returned and before the thread is destroyed.

property OnTerminate: TNotifyEvent;

Description

Write an OnTerminate event handler to execute code after the thread finishes executing. The OnTerminate event handler is called in the context of the main VCL thread, which means VCL methods and properties can be called freely. The thread object may also be freed within the event handler.
Assuming this to be true I made an procedure:
[pascal]
procedure TFileSearch.OnTerminate(Sender: TObject);
begin
SearchThread.Free;
SearchThread:=nil;
if Assigned(OnCompletion) then OnCompletion(Self);
end;
[/pascal]

And assigned OnCompletion to point to this:
[pascal]
procedure TForm1.OnDone(Sender: TObject);
begin
Caption:='Done '+IntToStr(ListBox1.Items.Count)+', time: '+IntToStr(GetTickCount-StartTime);
end;
[/pascal]

With this in place my test app will cause a very nasty EWin32Error exception (Access is denied) about 90% of the time (if I don't run it for a few minutes the first execution will be fine, but all later executions will all crash with this error).

There cause of the error is in the "Caption:='Done '+IntT...." statement. If I remove that statement everything works flawlessly. If I insert test code to retrieve the other values, such as this:

[pascal]
I:=GetTickCount-StartTime;
S:=IntToStr(ListBox1.Items.Count);
[/pascal]

It still works fine. However using this:
[pascal]
Caption:='Done';
[/pascal]
Will cause the crash, leading me to believe that for some reason OnTerminate is (despite what the help says) not 100% VCL safe, or at least not safe for the Win32 method which is used to change the caption of a Window (not a part of the VCL, but certainly used by the TForm VCL object).

So, has anyone else had any experience, and does anyone know a workaround?