Hello,
I'm stuck with quite trivial problem, but I cannot find answer anywhere.
Basically in my program I spawn TThreads dynamically. They do their jobs and then terminate. I would like to assign different callback function to each Thread.onTerminate.
But I don't know how to set that callback with dynamic arguments !
Let's explain this on example (rather pseudocode than real example):
type // this is just a helper object holding informations about jobs
TJob = object
ID: integer;
thread:TThread;
caption: string;
// (...)
end;
type TJobArray = array of TJob;
// global array
var g_Jobs: TJobArray;
// Here I have this procedure which spawns new thread on button click:
procedure StartNewJob(_caption:string);
var newThread: TThread;
var l:integer;
begin
SpawnNewThread(newThread);
l := Length(g_Jobs);
SetLength(g_Jobs, l + 1);
g_Jobs[l].thread :=newThread;
g_Jobs[l].ID := l;
g_Jobs[l].caption := _caption;
{ now I would like to do something like : }
newThread.OnTerminate := procedure(Sender: TObject)
begin
ShowFinishingMsg(ID, caption);
end;
{ which is an equivalent of anonymous function in JavaScript, but obviously doesn't work) }
end;
I know in this case I could pass ID and _caption to the Spawning function, which creates thread, but it's not the point (basically in my code each new Job also spawns new Tcontrol, and I want to modify each control at the end of each tread )
I want to assign a callback with different parameters to .onTerminate of each new thread and that's what I'm asking for.