Mega Code Archive

 
Categories / Delphi / Examples
 

Timercomponentdiy

> the old pascal delay method seems to gone fishing. > how do I do that in delphi 5? hi.. this is a common question. i see many people have suggested just using the sleep API. you could do this - but why not wrap it in a component and leverage your OO programming skills and delphi at the same time. below is a small component i wrote in D5 that does this. have fun mark (* this code is the property of Mark Meyer and LOGS Financial Service Inc., Northbrook, Illinois Copyright October 2000. this code is contributed to the Delphi developer community. it is submitted for public use "as is" and is meant to provide a basis on which others can learn and build. this code wraps the windows Sleep API into a D5 component and includes an OnBeforeSleep and OnAfterSleep event. if this code and component is of any benefit to you - please "keep the wheel turning" by helping others when and where you can in the programming community. if you have any questions please contact me. thx mark meyer wk: markm.hq@logs.com hm: geeky2@gte.net *) unit nap; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TBeforeSleepEvent = procedure(sender: TObject; var accept: Boolean) of object; TAfterSleepEvent = procedure(sender : TObject) of object; type TSleep = class(TComponent) private { Private declarations } FSleepTime: integer; FOnBeforeSleep: TBeforeSleepEvent; FOnAfterSleep: TAfterSleepEvent; function GetSleepTime : integer; procedure SetSleepTime (value : integer); protected { Protected declarations } public { Public declarations } constructor Create( AOwner: TComponent ); override; destructor Destroy; override; procedure GoToSleep; published { Published declarations } property SleepTime : integer read GetSleepTime write SetSleepTime; property OnBeforeSleep: TBeforeSleepEvent read FOnBeforeSleep write FOnBeforeSleep; property OnAfterSleep: TAfterSleepEvent read FOnAfterSleep write FOnAfterSleep; end; procedure Register; implementation constructor TSleep.Create( AOwner: TComponent ); begin inherited create(AOwner); FSleepTime := 1000; end; destructor TSleep.Destroy; begin inherited destroy; end; procedure TSleep.GoToSleep; var accept : boolean; begin accept := true; if Assigned(FOnBeforeSleep) then begin FOnBeforeSleep(Self, accept); end;{if} if (accept) then begin Sleep(SleepTime); end;{if} if Assigned(FOnAfterSleep) then begin FOnAfterSleep(Self); end;{if} end; function TSleep.GetSleepTime : integer; begin result := FSleepTime; end; procedure TSleep.SetSleepTime (value : integer); begin if (FSleepTime <> value) then begin FSleepTime := value; end;{if} end; procedure Register; begin RegisterComponents('LOGS', [TSleep]); end; end.