1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159:
| unit class_ThrDirectoryChangeNotification;
interface
uses Classes, Windows, Messages, Dialogs;
const
NOTIFY_DIRECTORY_TREE = TRUE; NOTIFY_SPECIFIED_DIRECTORY = FALSE;
type
TNotifyFilterType = ( FILE_NAME, FILE_DIR, FILE_ATTR, FILE_SIZE, FILE_LAST_WRITE, FILE_SECURITY );
TNotifyFilters = set of TNotifyFilterType;
TNotifyType = (OnlyDirectory,DirectoryTree);
Thr_DirectoryChangeNotification = class(TThread)
private
Filter : TNotifyFilters; hParent : THandle; hNotify : array of THandle; bool : Boolean;
public
constructor Create(CreateSuspended: Boolean; FileName: string; NotifyTyp: TNotifyType; NotifyFilter: TNotifyFilters; hOwner: THandle);
protected
procedure Execute; override;
end;
implementation
procedure Thr_DirectoryChangeNotification.Execute; var i: integer;
procedure MsgQueue; var Msg : TMsg; begin if PeekMessage(Msg,hParent,0,0,PM_REMOVE) then case Msg.message of WM_CLOSE, WM_QUIT : Terminate; end; end;
begin if High(hNotify) > -1 then try repeat case WaitForMultipleObjects( High(hNotify), @hNotify, FALSE, 1000 ) of WAIT_ABANDONED : begin ShowMessage('WAIT_ABANDONED'); end; WAIT_OBJECT_0 : begin FindNextChangeNotification(hNotify[0]); end; WAIT_OBJECT_0+1 : begin FindNextChangeNotification(hNotify[1]); end; WAIT_OBJECT_0+2 : begin FindNextChangeNotification(hNotify[2]); end; WAIT_OBJECT_0+3 : begin FindNextChangeNotification(hNotify[3]); end; WAIT_OBJECT_0+4 : begin FindNextChangeNotification(hNotify[4]); end; WAIT_OBJECT_0+5 : begin FindNextChangeNotification(hNotify[5]); end; WAIT_TIMEOUT : begin end; else begin end; end; MsgQueue; until terminated; finally for i:=low(hNotify) to high(hNotify) do FindCloseChangeNotification(hNotify[i]); end; end;
constructor Thr_DirectoryChangeNotification.Create(CreateSuspended: Boolean; FileName: string; NotifyTyp: TNotifyType; NotifyFilter: TNotifyFilters; hOwner: THandle); var a : array [0..5] of THandle; i : integer; const n : integer = 0; begin inherited Create(CreateSuspended); hParent := hOwner; case NotfyType of OnlyDirectory : bool := NOTIFY_SPECIFIED_DIRECTORY; DirectoryTree : bool := NOTIFY_DIRECTORY_TREE; end; if NotifyFilter <> [] then Filter := NotifyFilter else Filter := [FILE_NAME, FILE_DIR, FILE_ATTR, FILE_SIZE, FILE_LAST_WRITE]; for i:=0 to 5 do a[i] := INVALID_HANDLE_VALUE; if (FILE_NAME in Filter) then begin a[0]:= FindFirstChangeNotification(PChar(FileName),bool,FILE_NOTIFY_CHANGE_FILE_NAME); end; if (FILE_DIR in Filter) then begin a[1]:= FindFirstChangeNotification(PChar(FileName),bool,FILE_NOTIFY_CHANGE_DIR_NAME); end; if (FILE_ATTR in Filter) then begin a[2]:= FindFirstChangeNotification(PChar(FileName),bool,FILE_NOTIFY_CHANGE_ATTRIBUTES); end; if (FILE_SIZE in Filter) then begin a[3]:= FindFirstChangeNotification(PChar(FileName),bool,FILE_NOTIFY_CHANGE_SIZE); end; if (FILE_LAST_WRITE in Filter) then begin a[4]:= FindFirstChangeNotification(PChar(FileName),bool,FILE_NOTIFY_CHANGE_LAST_WRITE); end; if (FILE_SECURITY in Filter) then begin a[5]:= FindFirstChangeNotification(PChar(FileName),bool,FILE_NOTIFY_CHANGE_SECURITY); end; for i:=0 to 5 do if a[i] <> INVALID_HANDLE_VALUE then inc(n); SetLength(hNotify,n); n := 0; for i:=0 to 5 do if a[i] <> INVALID_HANDLE_VALUE then begin hNotify[n] := a[i]; inc(n); end; end;
end. |