wildzero
asked on
TThreadList - add and deleting
Hi there,
It's that time of the year again, Thread season!
Right so I got a Boss thread, which then creates a bunch of Worker threads.
In my boss thread I have
> FThreadList : TThreadList;
Which is created when the boss thread is created.
When the boss thread executes it creates a bunch of workers like so
Worker := TMyWorker.Create;
Worker.OnTerminate := OnWorkerThreadTerminate;
myList:= FThreadList.LockList;
try
id := myList.Add(Worker);
TMyWorker(myList.Items[id] ).Resume;
finally
FThreadList.UnlockList;
end;
Which is all good.
Now my problem is... when in my Boss.OnWorkerThreadTermina te how do I know which Worker Thread is terminating?
myList:= FThreadList.LockList;
try
myList.Delete(TMyWorker(se nder));
finally
FThreadList.UnlockList;
end;
that gives me no love (fails to compile).
Sorry for the silly questions, lack of sleep!
It's that time of the year again, Thread season!
Right so I got a Boss thread, which then creates a bunch of Worker threads.
In my boss thread I have
> FThreadList : TThreadList;
Which is created when the boss thread is created.
When the boss thread executes it creates a bunch of workers like so
Worker := TMyWorker.Create;
Worker.OnTerminate := OnWorkerThreadTerminate;
myList:= FThreadList.LockList;
try
id := myList.Add(Worker);
TMyWorker(myList.Items[id]
finally
FThreadList.UnlockList;
end;
Which is all good.
Now my problem is... when in my Boss.OnWorkerThreadTermina
myList:= FThreadList.LockList;
try
myList.Delete(TMyWorker(se
finally
FThreadList.UnlockList;
end;
that gives me no love (fails to compile).
Sorry for the silly questions, lack of sleep!
ASKER
actually it's
myList:= FThreadList.LockList;
try
id := TThread(sender).Handle;
For i := 0 to myList.Count-1 do
If TThread(myList.Items[i]).H andle = id then
begin
myList.Delete(i);
break;
end;
finally
FThreadList.UnlockList;
end;
myList:= FThreadList.LockList;
try
id := TThread(sender).Handle;
For i := 0 to myList.Count-1 do
If TThread(myList.Items[i]).H
begin
myList.Delete(i);
break;
end;
finally
FThreadList.UnlockList;
end;
procedure OnWorkerThreadTerminate(se nder: TObject); <-- ;-)
wouldn't "sender" be the thread?
wouldn't "sender" be the thread?
ASKER
Sender would be the 'worker' thread, which is why when this is done
TThread(sender).Handle
it's getting the handle of the worker thread.
TThread(sender).Handle
it's getting the handle of the worker thread.
oh, sorry, didn't hit "submit" for a while and you added some code.
yeah, that looks ok unless you have "FreeOnTerminate := true" I think.
Although I did get an occasional error on my test app with several thousand iterations (threads just doing a random sleep) hmm...
yeah, that looks ok unless you have "FreeOnTerminate := true" I think.
Although I did get an occasional error on my test app with several thousand iterations (threads just doing a random sleep) hmm...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
dinilud - I ended up going with that code. Just broke it out a bit so it would check that the indexof was actually > -1 (just in case). Indexof is rated faster then just doing a normal for loop aparently.
ASKER
myList:= FThreadList.LockList;
try
id := TMyWorker(sender).Handle;
I := myList.IndexOf(id);
If i > -1 then
begin
myList.Delete(I);
end;
finally
FThreadList.UnlockList;
end;
That seem OK, anyone got anything better.