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.OnWorkerThreadTerminate how do I know which Worker Thread is terminating?

  myList:= FThreadList.LockList;
  try
    myList.Delete(TMyWorker(sender));
  finally
    FThreadList.UnlockList;
  end;

that gives me no love (fails to compile).

Sorry for the silly questions, lack of sleep!
LVL 10
wildzeroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wildzeroAuthor Commented:
This is what I have done to delete and it seems to work.

  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.
0
wildzeroAuthor Commented:
actually it's

  myList:= FThreadList.LockList;
  try
    id := TThread(sender).Handle;
    For i := 0 to myList.Count-1 do
      If TThread(myList.Items[i]).Handle = id then
        begin
          myList.Delete(i);
          break;
        end;
  finally
    FThreadList.UnlockList;
  end;
0
TheRealLokiSenior DeveloperCommented:
procedure OnWorkerThreadTerminate(sender: TObject); <--   ;-)
wouldn't "sender" be the thread?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

wildzeroAuthor Commented:
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.

0
TheRealLokiSenior DeveloperCommented:
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...
0
diniludCommented:
try like this

myList:= FThreadList.LockList;
  try
    myList.Delete(myList..IndexOf(TMyWorker(sender)));
  finally
    FThreadList.UnlockList;
  end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wildzeroAuthor Commented:
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.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.