IdThreadMgr.pas 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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: 10379: IdThreadMgr.pas
  11. {
  12. { Rev 1.0 2002.11.12 10:56:08 PM czhower
  13. }
  14. unit IdThreadMgr;
  15. (*
  16. Changes
  17. 02 Oct 2001 - Allen O'Neill - Added support for thread priority - new property Threadpriority, new line added to OnCreate {Do not Localize}
  18. *)
  19. interface
  20. uses
  21. Classes,
  22. IdException, IdBaseComponent, IdGlobal, IdThread,
  23. SyncObjs;
  24. type
  25. TIdThreadMgr = class(TIdBaseComponent)
  26. protected
  27. FActiveThreads: TThreadList;
  28. FThreadClass: TIdThreadClass;
  29. FThreadPriority: TIdThreadPriority;
  30. public
  31. constructor Create(AOwner: TComponent); override;
  32. function CreateNewThread: TIdThread; virtual;
  33. destructor Destroy; override;
  34. function GetThread: TIdThread; virtual; abstract;
  35. procedure ReleaseThread(AThread: TIdThread); virtual; abstract;
  36. procedure TerminateThreads; virtual;
  37. //
  38. property ActiveThreads: TThreadList read FActiveThreads;
  39. property ThreadClass: TIdThreadClass read FThreadClass write FThreadClass;
  40. property ThreadPriority: TIdThreadPriority read FThreadPriority
  41. write FThreadPriority default tpNormal;
  42. end;
  43. EIdThreadMgrError = class(EIdException);
  44. EIdThreadClassNotSpecified = class(EIdThreadMgrError);
  45. implementation
  46. uses
  47. IdResourceStrings, IdTCPServer,
  48. SysUtils;
  49. { TIdThreadMgr }
  50. constructor TIdThreadMgr.Create(AOwner: TComponent);
  51. begin
  52. inherited Create(AOwner);
  53. FActiveThreads := TThreadList.Create;
  54. FThreadPriority := tpNormal;
  55. end;
  56. function TIdThreadMgr.CreateNewThread: TIdThread;
  57. begin
  58. if ThreadClass = nil then begin
  59. raise EIdThreadClassNotSpecified.create(RSThreadClassNotSpecified);
  60. end;
  61. Result := TIdThreadClass(ThreadClass).Create;
  62. SetThreadPriority(Result, ThreadPriority);
  63. end;
  64. destructor TIdThreadMgr.Destroy;
  65. begin
  66. FreeAndNil(FActiveThreads);
  67. inherited Destroy;
  68. end;
  69. procedure TIdThreadMgr.TerminateThreads;
  70. begin
  71. //
  72. end;
  73. end.