Delphi 监控文件改变
来源:互联网 发布:图片推荐算法 编辑:程序博客网 时间:2024/04/30 08:46
unit FileSysThread;interfaceuses Windows, SysUtils, Classes, comctrls;type TFileSysNotifyThread = class(TThread) private ErrCode: Integer; KillAddress: PInteger; NotifyHandle: THandle; WatchPath: string; WatchMask: Integer; procedure SignalFileNotification; protected procedure Execute; override; public constructor Create(const AWatchPath: string; AWatchMask: Integer; var Myself: TFileSysNotifyThread); destructor Destroy; override; end;implementationuses Dialogs;constructor TFileSysNotifyThread.Create(const AWatchPath: string; AWatchMask: Integer; var Myself: TFileSysNotifyThread);begin inherited Create(True); WatchPath := AWatchPath; WatchMask := AWatchMask; KillAddress := Addr(Myself); Priority := tpLower; FreeOnTerminate := True; Suspended := False;end;destructor TFileSysNotifyThread.Destroy;begin if NotifyHandle <> THandle(-1) then FindCloseChangeNotification(NotifyHandle); inherited Destroy; KillAddress^ := 0;end;procedure TFileSysNotifyThread.Execute;begin NotifyHandle := FindFirstChangeNotification(PChar(WatchPath), True, WatchMask); if NotifyHandle <> THandle(-1) then while not Terminated do begin ErrCode := WaitForSingleObject(NotifyHandle, 250); case ErrCode of Wait_Timeout: ; Wait_Object_0: begin Synchronize(SignalFileNotification); FindNextChangeNotification(NotifyHandle); end; else ; end; end;end;procedure TFileSysNotifyThread.SignalFileNotification;begin ShowMessage('文件已改变! ');end;end.