• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1903
  • Last Modified:

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!
0
wildzero
Asked:
wildzero
  • 4
  • 2
  • 2
1 Solution
 
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
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
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
 
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now