A bit OOT, how to call procedure ThreadDone with parameter? Is it possible?
Not OT, and yes, it is possible. But not in the 'normal' way.
If you look at the TNotifyEvent as it is defined now you find:
TNotifyEvent = procedure(Sender: TObject) of object;
So you see there is already one parameter you needed to provide.
When you want more/other parameters you need to define another procedure of your own.
Make sure it matches what you want exactly.
But the OnTerminate itself is private and can't be changed:
procedure TThread.CallOnTerminate;
begin
FOnTerminate(Self);
end;
So you can't any new parameters there.
You can create your own FMyTerminate methods.
Something like this.
But note that there is a simpler way (see below).type
TMyNotifyEvent = procedure(Sender: TObject; Thread:integer) of object;
type
TD3GetDtThr = class(TThread)
private
FMyTerminate: TMyNotifyEvent;
procedure CallMyTerminate;
protected
procedure Execute; override;
procedure DoTerminate; override; // we override DoTerminate to include calling our own MyTerminate
public
constructor Create(CreateSuspended: boolean; AThreadDone: TMyNotifyEvent); overload;
property MyTerminate: TMyNotifyEvent read FMyTerminate write FMyTerminate;
end;
constructor TD3GetDtThr.Create(CreateSuspended: boolean; AThreadDone: TMyNotifyEvent);
begin
inherited Create(True);
FreeOnTerminate := True;
MyTerminate := AThreadDone; // prefix with @ if in Delphi mode
if not CreateSuspended then
Start;
end;
procedure TD3GetDtThr.Execute;
begin
//
end;
procedure TD3GetDtThr.CallMyTerminate;
begin
FMyTerminate(Self, 12345); // This is our own OnTerminate
end;
procedure TD3GetDtThr.DoTerminate;
begin
if Assigned(FMyTerminate) then
Synchronize(@CallMyTerminate);
inherited;
end;
But as I said... it might be even simpler depending on your own code.
The TNotifyEvent has a Sender. The Sender of OnTerminate is always the TThread itself.
As can be seen from the source earlier.
FOnTerminate(Self); // the OnTerminate in the Thread passes itself to your OnTerminate procedure !!
So in your own ThreadDone you can access the Thread. Just make sure any variable you need are public.
procedure TMainFrm.ThreadDone(Sender: TObject);
begin
if Sender is TD3GetDtThr then
begin
// here you can access TD3GetDtThr(Sender).Public_Variables
end;
end;
Much simpler, isn't it
(Maybe I didn't need to explain the whole other method... but hey, we are here to learn
)
Please report a bug towards the documentation so that it isn't forgotten.
Done