IdTask.pas 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.2 2003.11.04 3:49:00 PM czhower
  18. Update to sync TC
  19. Rev 1.1 2003.10.21 12:19:02 AM czhower
  20. TIdTask support and fiber bug fixes.
  21. }
  22. unit IdTask;
  23. interface
  24. {$i IdCompilerDefines.inc}
  25. uses
  26. IdGlobal,
  27. IdYarn,
  28. SysUtils;
  29. type
  30. TIdTask = class(TObject)
  31. protected
  32. FBeforeRunDone: Boolean;
  33. // When ARC is enabled, object references MUST be valid objects.
  34. // It is common for users to store non-object values, though, so
  35. // we will provide separate properties for those purposes
  36. FDataObject: TObject;
  37. FDataValue: PtrInt;
  38. FYarn: TIdYarn;
  39. //
  40. procedure AfterRun; virtual;
  41. procedure BeforeRun; virtual;
  42. function Run: Boolean; virtual; abstract;
  43. procedure HandleException(AException: Exception); virtual;
  44. public
  45. constructor Create(AYarn: TIdYarn); reintroduce; virtual;
  46. destructor Destroy; override;
  47. // The Do's are separate so we can add events later if necessary without
  48. // needing the inherited calls to perform them, as well as allowing
  49. // us to keep the real runs as protected
  50. procedure DoAfterRun;
  51. procedure DoBeforeRun;
  52. function DoRun: Boolean;
  53. procedure DoException(AException: Exception);
  54. // BeforeRunDone property to allow flexibility in alternative schedulers
  55. property BeforeRunDone: Boolean read FBeforeRunDone;
  56. //
  57. property DataObject: TObject read FDataObject write FDataObject;
  58. property DataValue: PtrInt read FDataValue write FDataValue;
  59. {$IFNDEF USE_OBJECT_ARC}
  60. property Data: TObject read FDataObject write FDataObject; // deprecated 'Use DataObject or DataValue property instead.';
  61. {$ENDIF}
  62. property Yarn: TIdYarn read FYarn;
  63. end;
  64. implementation
  65. uses
  66. IdGlobal;
  67. { TIdTask }
  68. procedure TIdTask.AfterRun;
  69. begin
  70. end;
  71. procedure TIdTask.BeforeRun;
  72. begin
  73. end;
  74. procedure TIdTask.HandleException(AException: Exception);
  75. begin
  76. end;
  77. constructor TIdTask.Create(AYarn: TIdYarn);
  78. begin
  79. inherited Create;
  80. FYarn := AYarn;
  81. FBeforeRunDone := False;
  82. end;
  83. destructor TIdTask.Destroy;
  84. begin
  85. // Dont free the yarn, that is the responsibilty of the thread / fiber.
  86. // .Yarn here is just a reference, not an ownership
  87. FDataObject.Free;
  88. FDataValue := 0;
  89. inherited Destroy;
  90. end;
  91. procedure TIdTask.DoAfterRun;
  92. begin
  93. AfterRun;
  94. end;
  95. procedure TIdTask.DoBeforeRun;
  96. begin
  97. FBeforeRunDone := True;
  98. BeforeRun;
  99. end;
  100. function TIdTask.DoRun: Boolean;
  101. begin
  102. Result := Run;
  103. end;
  104. procedure TIdTask.DoException(AException: Exception);
  105. begin
  106. HandleException(AException);
  107. end;
  108. end.