2018-01-25 21:34:04 +01:00
|
|
|
unit uCEFWorkSchedulerThread;
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE OBJFPC}{$H+}
|
|
|
|
{$ENDIF}
|
|
|
|
|
2018-01-25 21:34:04 +01:00
|
|
|
{$I cef.inc}
|
|
|
|
|
2022-02-19 18:56:41 +01:00
|
|
|
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
|
|
|
|
{$MINENUMSIZE 4}
|
|
|
|
|
2018-01-25 21:34:04 +01:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
System.Classes, System.SyncObjs,
|
|
|
|
{$ELSE}
|
|
|
|
Classes, SyncObjs,
|
|
|
|
{$ENDIF}
|
|
|
|
uCEFConstants;
|
|
|
|
|
|
|
|
type
|
|
|
|
TCEFWorkSchedulerThread = class(TThread)
|
|
|
|
protected
|
|
|
|
FCritSect : TCriticalSection;
|
|
|
|
FInterval : integer;
|
|
|
|
FEvent : TEvent;
|
|
|
|
FOnPulse : TNotifyEvent;
|
|
|
|
FPulsing : boolean;
|
|
|
|
FMustReset : boolean;
|
|
|
|
FDefaultInterval : integer;
|
|
|
|
|
|
|
|
function Lock : boolean;
|
|
|
|
procedure Unlock;
|
|
|
|
function CanPulse(var aInterval : integer) : boolean;
|
|
|
|
procedure DoOnPulseEvent;
|
|
|
|
procedure EventTimeOut;
|
|
|
|
procedure SignaledEvent;
|
|
|
|
procedure Execute; override;
|
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
procedure AfterConstruction; override;
|
|
|
|
procedure NextPulse(aInterval : integer);
|
|
|
|
|
|
|
|
property DefaultInterval : integer read FDefaultInterval write FDefaultInterval default CEF_TIMER_MAXDELAY;
|
|
|
|
property OnPulse : TNotifyEvent read FOnPulse write FOnPulse;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2021-05-27 14:29:30 +02:00
|
|
|
uCEFMiscFunctions,
|
2018-01-25 21:34:04 +01:00
|
|
|
{$IFDEF DELPHI16_UP}
|
2019-11-08 23:15:53 +01:00
|
|
|
System.SysUtils, System.Math;
|
2018-01-25 21:34:04 +01:00
|
|
|
{$ELSE}
|
2019-11-08 23:15:53 +01:00
|
|
|
SysUtils, Math;
|
2018-01-25 21:34:04 +01:00
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
constructor TCEFWorkSchedulerThread.Create;
|
|
|
|
begin
|
|
|
|
FOnPulse := nil;
|
|
|
|
FCritSect := nil;
|
|
|
|
FPulsing := False;
|
|
|
|
FEvent := nil;
|
|
|
|
FDefaultInterval := CEF_TIMER_MAXDELAY;
|
|
|
|
FInterval := FDefaultInterval;
|
|
|
|
FMustReset := False;
|
|
|
|
|
|
|
|
inherited Create(True);
|
|
|
|
|
|
|
|
FreeOnTerminate := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TCEFWorkSchedulerThread.Destroy;
|
|
|
|
begin
|
|
|
|
if (FEvent <> nil) then FreeAndNil(FEvent);
|
|
|
|
if (FCritSect <> nil) then FreeAndNil(FCritSect);
|
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.AfterConstruction;
|
|
|
|
begin
|
|
|
|
inherited AfterConstruction;
|
|
|
|
|
|
|
|
FEvent := TEvent.Create(nil, False, False, '');
|
|
|
|
FCritSect := TCriticalSection.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.DoOnPulseEvent;
|
|
|
|
begin
|
|
|
|
if assigned(FOnPulse) then FOnPulse(self);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCEFWorkSchedulerThread.Lock : boolean;
|
|
|
|
begin
|
|
|
|
if not(Terminated) and (FCritSect <> nil) then
|
|
|
|
begin
|
|
|
|
FCritSect.Acquire;
|
|
|
|
Result := True;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.Unlock;
|
|
|
|
begin
|
|
|
|
if (FCritSect <> nil) then FCritSect.Release;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.NextPulse(aInterval : integer);
|
|
|
|
begin
|
|
|
|
if Lock then
|
|
|
|
try
|
|
|
|
FInterval := min(aInterval, CEF_TIMER_MAXDELAY);
|
|
|
|
FMustReset := True;
|
|
|
|
|
|
|
|
if FPulsing then
|
|
|
|
begin
|
|
|
|
FPulsing := False;
|
|
|
|
FEvent.SetEvent;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
Unlock;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.EventTimeOut;
|
|
|
|
begin
|
|
|
|
if Lock then
|
|
|
|
try
|
|
|
|
if FMustReset then
|
|
|
|
begin
|
|
|
|
FInterval := FDefaultInterval;
|
|
|
|
FMustReset := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
FPulsing := False;
|
|
|
|
finally
|
|
|
|
Unlock;
|
2021-05-26 19:32:10 +02:00
|
|
|
|
|
|
|
if not(Terminated) then
|
|
|
|
Synchronize({$IFDEF FPC}self, @{$ENDIF}DoOnPulseEvent);
|
2018-01-25 21:34:04 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.SignaledEvent;
|
|
|
|
begin
|
|
|
|
if Lock then
|
|
|
|
try
|
|
|
|
FPulsing := False;
|
|
|
|
finally
|
|
|
|
Unlock;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCEFWorkSchedulerThread.CanPulse(var aInterval : integer) : boolean;
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
|
|
|
|
if Lock then
|
|
|
|
try
|
|
|
|
aInterval := FInterval;
|
|
|
|
|
|
|
|
if (aInterval > 0) then
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
FPulsing := True;
|
|
|
|
FEvent.ResetEvent;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
Unlock;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFWorkSchedulerThread.Execute;
|
|
|
|
var
|
|
|
|
TempInterval : integer;
|
|
|
|
begin
|
|
|
|
while CanPulse(TempInterval) do
|
|
|
|
if (FEvent.WaitFor(TempInterval) = wrTimeout) then
|
|
|
|
EventTimeOut
|
|
|
|
else
|
|
|
|
SignaledEvent;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|