Mozes da koristis tzv. change notifikacije koristeci windows API. Preko Find(First/Next)ChangeNotification dobijes win handle od waitobj-a koji prati promene datoteke i onda cekas na promene sa WaitForSingleObject. Jedini problem je to sto je WaitForSingleObject tzv. blocking poziv, sto znaci da ce ti blokirati glavni thread ako ga pozoves iz njega (sto i nije neki problem ako program samo to radi, npr. servisna aplikacija koja ceka na promenu, onda nesto cacka pa onda ceka ne sledecu promenu...). Logicno resenje ja da cekas iz worker thread-a koji nece blokirati glavni thread. Evo ti kompletna klasa koja to radi:
Code:
unit FileMonitor;
interface
uses
Classes, Windows, SysUtils;
type
TFileMonitor = class(TThread)
private
FFileName: String;
{ Private declarations }
protected
procedure Execute; override;
procedure DoSomething;
public
destructor Destroy; override;
procedure StartMonitoring(Filename: String);
end;
implementation
{ TFileMonitor }
destructor TFileMonitor.Destroy;
begin
inherited;
end;
procedure TFileMonitor.DoSomething;
begin
//tvoj kod za obradu promene
end;
procedure TFileMonitor.Execute;
var
H: Cardinal;
begin
H := FindFirstChangeNotification(PChar(FFileName), False, FILE_NOTIFY_CHANGE_LAST_WRITE or FILE_NOTIFY_CHANGE_FILE_NAME);
if H = INVALID_HANDLE_VALUE then
Terminate
else
try
while not Terminated do
begin
WaitForSingleObject(H, INFINITE);
if not FileExists(FFileName) then
begin
Terminate;
break;
end;
Synchronize(DoSomething);
if not FindNextChangeNotification(H) then
begin
Terminate;
break;
end;
end;
finally
FindCloseChangeNotification(H);
end;
end;
procedure TFileMonitor.StartMonitoring(FileName: String);
begin
FFileName := FileName;
Self.FreeOnTerminate := True;
Resume;
end;
end.
Klasu koristis sa
Code:
TFileMonitor.Create.StartMonitoring('C:\Path\File.ext');
Klasa je fire-and-forget jer koristi FreeOnTerminate.
Medjutim izgleda mi da tebi ovo treba da bi ostvario komunikaciju izmedju dva programa. Ako si ti autor oba programa onda je mozda bolje da potrazis nesto na temu memory mapped files ili shared memory, daleko bolji nacin za interprocesnu komunikaciju na istoj masini.