IdThreadMgrDefault.pas 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10381: IdThreadMgrDefault.pas
  11. {
  12. { Rev 1.0 2002.11.12 10:56:14 PM czhower
  13. }
  14. unit IdThreadMgrDefault;
  15. interface
  16. uses
  17. IdThread, IdThreadMgr;
  18. type
  19. TIdThreadMgrDefault = class(TIdThreadMgr)
  20. public
  21. function GetThread: TIdThread; override;
  22. procedure ReleaseThread(AThread: TIdThread); override;
  23. end;
  24. implementation
  25. uses
  26. IdGlobal;
  27. { TIdThreadMgrDefault }
  28. function TIdThreadMgrDefault.GetThread: TIdThread;
  29. begin
  30. Result := CreateNewThread;
  31. ActiveThreads.Add(result);
  32. end;
  33. procedure TIdThreadMgrDefault.ReleaseThread(AThread: TIdThread);
  34. begin
  35. with ActiveThreads.LockList do try // To avoid ReleaseThread-code is
  36. if IndexOf(AThread)=-1 then exit; // executed multiple times, because
  37. finally // AThread.Free call ReleaseThread
  38. ActiveThreads.UnlockList; // again. Now we will detect the 2nd
  39. end; // time, and jump out of the routine
  40. ActiveThreads.Remove(AThread); // here.
  41. if not IsCurrentThread(AThread) then begin
  42. // Test suspended and not stopped - it may be in the process of stopping.
  43. if not AThread.Suspended then begin
  44. AThread.TerminateAndWaitFor;
  45. end;
  46. AThread.Free;
  47. end else begin
  48. AThread.FreeOnTerminate := True;
  49. AThread.Terminate; //APR: same reason as MgrPool. ELSE threads leak if smSuspend
  50. end;
  51. end;
  52. end.