Delphi 监控文件改变

 
unit FileSysThread;

interface

uses
  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;

implementation

uses 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.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值