/components/jcf2/Utils/Delay.pas

http://github.com/graemeg/lazarus · Pascal · 166 lines · 72 code · 31 blank · 63 comment · 4 complexity · d62a95fea3d52d2b69a4076628745b14 MD5 · raw file

  1. unit Delay;
  2. { AFS 14 Jan 2K
  3. abstraction of timer mechanism
  4. Use this to call a proc after a short delay
  5. Needed for IDE regiestering
  6. See delayed reg. technique from sample code by Mike Remec
  7. http://www.miharemec.com/doc/ota-nmi.html
  8. usage:
  9. lcMyDelay := TDelay.Create;
  10. lcMyDelay.ObjectProc := lcSomeObject.Proc;
  11. lcMyDelay.DoItSoon;
  12. ....
  13. lcMyDelay.Free;
  14. or
  15. lcMyDelay := TDelay.Create;
  16. lcMyDelay.Proc := SomeProc;
  17. lcMyDelay.DoItSoon;
  18. ....
  19. lcMyDelay.Free;
  20. }
  21. {(*}
  22. (*------------------------------------------------------------------------------
  23. Delphi Code formatter source code
  24. The Original Code is Delay, released May 2003.
  25. The Initial Developer of the Original Code is Anthony Steele.
  26. Portions created by Anthony Steele are Copyright (C) 1999-2000 Anthony Steele.
  27. All Rights Reserved.
  28. Contributor(s): Anthony Steele.
  29. The contents of this file are subject to the Mozilla Public License Version 1.1
  30. (the "License"). you may not use this file except in compliance with the License.
  31. You may obtain a copy of the License at http://www.mozilla.org/NPL/
  32. Software distributed under the License is distributed on an "AS IS" basis,
  33. WITHOUT WARRANTY OF ANY KIND, either express or implied.
  34. See the License for the specific language governing rights and limitations
  35. under the License.
  36. Alternatively, the contents of this file may be used under the terms of
  37. the GNU General Public License Version 2 or later (the "GPL")
  38. See http://www.gnu.org/licenses/gpl.html
  39. ------------------------------------------------------------------------------*)
  40. {*)}
  41. {$I JcfGlobal.inc}
  42. interface
  43. uses ExtCtrls;
  44. type
  45. TProcedure = procedure(var pbTryAgain: boolean);
  46. TObjectProcedure = procedure(var pbTryAgain: boolean) of object;
  47. TDelay = class(TObject)
  48. private
  49. fiDelay: integer;
  50. // can call a proc, or a proc on an object (or both)
  51. fcProc: TProcedure;
  52. fcObjectProc: TObjectProcedure;
  53. fcTimer: TTimer;
  54. fbDone: boolean;
  55. procedure DoItNow(Sender: TObject);
  56. public
  57. constructor Create;
  58. destructor Destroy; override;
  59. procedure DoItSoon;
  60. { how long to delay in Miliseconds}
  61. property Delay: integer Read fiDelay Write fiDelay;
  62. { done yet? }
  63. property Done: boolean Read fbDone;
  64. { proc to call }
  65. property Proc: TProcedure Read fcProc Write fcProc;
  66. property objectProc: TobjectProcedure Read fcObjectProc Write fcObjectProc;
  67. end;
  68. implementation
  69. uses SysUtils;
  70. const
  71. // default of 100ms = 1/0 second
  72. DEFAULT_DELAY = 100;
  73. { TDelay }
  74. constructor TDelay.Create;
  75. begin
  76. inherited;
  77. fcTimer := nil; // create the timer when needed
  78. fcProc := nil;
  79. fcObjectProc := nil;
  80. fiDelay := DEFAULT_DELAY; // default 1/2 sec
  81. fbDone := False;
  82. end;
  83. destructor TDelay.Destroy;
  84. begin
  85. FreeAndNil(fcTimer);
  86. inherited;
  87. end;
  88. procedure TDelay.DoItNow(Sender: TObject);
  89. var
  90. lbDoAgain: boolean;
  91. begin
  92. Assert(fcTimer <> nil);
  93. lbDoAgain := False;
  94. // disable until finished
  95. fcTimer.Enabled := False;
  96. if Assigned(fcProc) then
  97. fcProc(lbDoAgain);
  98. if assigned(fcObjectProc) then
  99. fcObjectProc(lbDoAgain);
  100. //FreeAndNil(fcTimer); this causes problems in IDE plug-ins
  101. // stop unless the proc called raised the falg
  102. if lbDoAgain then
  103. begin
  104. // restart
  105. fcTimer.Enabled := True;
  106. end
  107. else
  108. begin
  109. fbDone := True;
  110. { no longer timing }
  111. fcTimer.OnTimer := nil;
  112. end;
  113. end;
  114. procedure TDelay.DoItSoon;
  115. begin
  116. // need a timer now
  117. if fcTimer = nil then
  118. fcTimer := TTimer.Create(nil);
  119. fcTimer.Interval := fiDelay;
  120. fcTimer.OnTimer := DoItNow;
  121. fcTimer.Enabled := True;
  122. fbDone := False;
  123. end;
  124. end.