⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesysthread.pas

📁 在delphi中实现windows核心编程.原书光盘代码核心编程.原书光盘代码
💻 PAS
字号:
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), False, 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.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -