Directory-Monitor

This commit is contained in:
Michael Schimmel
2025-07-17 10:30:08 +02:00
parent 0b891c6def
commit 208006c896
4 changed files with 663 additions and 1 deletions
+41
View File
@@ -14,6 +14,7 @@ type
procedure CreateThread(const Proc: TProc);
procedure RunTask(const Gate: TState; const Proc: TProc);
procedure WaitFor(const State: TState);
function InWorkerThread: Boolean;
end;
private
FTaskManager: ITaskManager;
@@ -27,8 +28,16 @@ type
// Returns a State to await thread completion.
function CreateThread(const Proc: TProc): TState;
// Synchronize from independent thread (created by CreateThread). This is a blocking function and it will
// raise an exception, if it is called called from a task (created by RunTask).
procedure Synchronize(const Proc: TProc);
// true, if called from a task (created by RunTask)
function InTask: Boolean;
// Run a task when the gate is opened.
// Returns a State to await thread completion.
// No blocking operations are allowed whithin tasks!
function RunTask(const Proc: TFunc<TState>): TState; overload;
function RunTask(const Gate: TState; const Proc: TFunc<TState>): TState; overload;
procedure RunTask(const Gate: TState; const Proc: TProc); overload;
@@ -62,6 +71,7 @@ procedure SetupTaskManagerMock;
implementation
uses
System.Classes,
Myc.Core.Tasks;
type
@@ -79,6 +89,7 @@ type
procedure RunTask(const Gate: TState; const Proc: TProc);
procedure WaitFor(const State: TState);
procedure CreateThread(const Proc: TProc);
function InWorkerThread: Boolean;
end;
{ TMycExecMock }
@@ -111,6 +122,11 @@ begin
Proc();
end;
function TMycTaskManagerMock.InWorkerThread: Boolean;
begin
Result := false;
end;
procedure TMycTaskManagerMock.RunTask(const Gate: TState; const Proc: TProc);
begin
Gate.Signal.Subscribe(TMycExecMock.Create(Proc));
@@ -151,6 +167,31 @@ begin
);
end;
function TTaskManager.InTask: Boolean;
begin
Result := FTaskManager.InWorkerThread;
end;
procedure TTaskManager.Synchronize(const Proc: TProc);
begin
if InTask then
raise Exception.Create('Synchronization in tasks not allowed');
var cProc := Proc;
if Assigned(cProc) then
TThread.Synchronize(
nil,
procedure
begin
if Assigned(cProc) then
begin
cProc();
cProc := nil;
end;
end
);
end;
class function TTaskManager.RunSequence(const Gate: TState; First, Count: Integer; const Proc: TFunc<Integer, TState>): TState;
begin
if First >= Count then